簡體   English   中英

春季安全性注銷電話

[英]Spring security logout call

我正在嘗試使用Java配置來設置Spring Security + mvc,但是由於某種原因它無法正常工作,我收到了404錯誤。

在實現的WebApplicationInitializer類中,我以另一種方式注冊安全過濾器

 @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
...
       FilterRegistration.Dynamic securityFilterChain = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
        securityFilterChain.addMappingForUrlPatterns(null, false, "/*");
..

列出SecurityContext

@Configuration
@EnableWebSecurity
public class SecurityContext extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/assets/**").permitAll()
                    .antMatchers("/admin/**").hasRole("ADMIN")
                    .antMatchers("/profile/**").hasAnyRole("ADMIN", "USER")
                .and()
                    .formLogin()
                        .loginPage("/login")
                        .defaultSuccessUrl("/profile")
                        .failureUrl("/login?error")
                        .usernameParameter("username")
                        .passwordParameter("password")
                        .permitAll()
//                .and()
//                    .logout()
//                    .logoutUrl("/logout")
//                    .logoutSuccessUrl("/")
//                    .permitAll()
                .and()
                    .exceptionHandling().accessDeniedPage("/403");
     }
}

對於logoutUrl,我嘗試了所有組合,但是沒有運氣...當我嘗試在我的jsp頁面中使用此鏈接時

<c:url value='/j_spring_security_check' />

我收到404找不到異常。

我花了整整一天的時間使它工作。 有誰知道如何解決這個問題?

PS:例如,如果我將logoutUrl設置為“ / logout”,是否應該讓contoller處理該URL?

您的登出機制不起作用...是否意味着您的登入機制能正常工作? 確實,在這種情況下,請嘗試處理您的'/ logOut'網址:

public LogInController{
...

    @RequestMapping(value = "/logOut", method = RequestMethod.GET)
    public String logOut(ModelMap model) {

    //Redirect to your start page (mapping the url '/welcome' for example)
    return "redirect:welcome";
    }
...
}

如果不是,請檢查是否已將安全性配置文件添加到“ onStartup”方法中:

public void onStartup(ServletContext servletContext) throws ServletException {


 AnnotationConfigWebApplicationContext rootContext =
                new AnnotationConfigWebApplicationContext();

        //adding your main config class 
        rootContext.register(WebAppConfig.class);

        //adding your security config class
        rootContext.register(SecurityConfiguration.class);
...

}

然后,您可以嘗試在http之后添加。 在“配置”方法中(如果您在授權之前不使用csrf令牌):

 csrf().disable()

並檢查其他豆:

@Bean
public ProviderManager providerManager() {
    List<AuthenticationProvider> list = new ArrayList<AuthenticationProvider>();
    list.add(daoAuthenticationProvider());
    return new ProviderManager(list);
}

//If you use this filter (I think so, because you've defined 'username' and 'password' in
'configure' method)
@Bean
public UsernamePasswordAuthenticationFilter filter() {
    UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter();
    filter.setAuthenticationManager(providerManager());
    return filter;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM