简体   繁体   中英

Spring security logout call

I'm trying to setup Spring Security + mvc using Java config, but for some reason it's not working, I getting a 404 error.

In my implemented WebApplicationInitializer class I register security filter next way

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

Listing of 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");
     }
}

For the logoutUrl I've tried all combinations with no luck... When i'm trying to use this link in my jsp page

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

I'm getting 404 not found exception.

I've spent whole day trying to make it work. Is anyone have an ideas how to solve this issue?

PS If I'll set logoutUrl to "/logout" for example, should I make a contoller to handle this url?

Your logOut mechanizm doesn't work... Is it means that your logIn mechanizm works right? In this case, realy, try to handle your '/logOut' url:

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";
    }
...
}

If not, check whether you have added your Security configuration file to your 'onStartup' method:

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);
...

}

Then you can try add after http. in 'configure' method this (if you don't use csrf tokens till authorization):

 csrf().disable()

And check other beans:

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM