简体   繁体   English

重定向注销Spring Security

[英]Redirect for logout Spring Security

I use the following Spring security configuraiton for my @Override 我为@Override使用以下Spring安全配置

protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/login").permitAll()
        .and()
        .authorizeRequests()
            .antMatchers("/signup").permitAll()
        .and()
        .authorizeRequests()
            .anyRequest().authenticated()
        .and()
            .logout().logoutUrl("/logout").logoutSuccessUrl("/login").deleteCookies("auth_code").invalidateHttpSession(true)
        .and()
        // We filter the api/signup requests
        .addFilterBefore(
            new JWTSignupFilter("/signup", authenticationManager(),
                    accountRepository, passwordEncoder),
            UsernamePasswordAuthenticationFilter.class)
        // We filter the api/login requests
        .addFilterBefore(
            new JWTLoginFilter("/login", authenticationManager()),
            UsernamePasswordAuthenticationFilter.class)
        // And filter other requests to check the presence of JWT in
        // header
        .addFilterBefore(new JWTAuthenticationFilter(userDetailsServiceBean()),
            UsernamePasswordAuthenticationFilter.class);
}

I want browser to be redirected to /login URL in result of successful logout. 我希望浏览器成功注销后重定向到/login URL。 But I get this response: 但我得到了这样的答复:

{
  "timestamp": 1493871686489,
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/login"
}

Edit 1 编辑1

I have a login filter which captures POST requests to the /login endpoint: 我有一个登录过滤器,它捕获对/login端点的POST请求:

public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {


    public JWTLoginFilter(String url, AuthenticationManager authManager) {
        super(new AntPathRequestMatcher(url, "POST"));
        setAuthenticationManager(authManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest req,
            HttpServletResponse res) throws AuthenticationException,
            IOException, ServletException {

        CustomUserDetails creds = new ObjectMapper().readValue(
                req.getInputStream(), CustomUserDetails.class);

        return getAuthenticationManager().authenticate(
                new UsernamePasswordAuthenticationToken(creds.getUsername(),
                        creds.getPassword()));
    }

    @Override
    protected void successfulAuthentication(HttpServletRequest req,
            HttpServletResponse res, FilterChain chain, Authentication auth) {
        TokenAuthenticationService.addAuthentication(res, auth.getName());
    }
}

EDIT 2 编辑2

The following rest controller doesn't get hit ! 以下休息控制器不会被击中!

@RestController
public class UserAcccountController {
    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(path = "/login", method = RequestMethod.GET)
    public String loginGet() {
        return "/login";
    }

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public void loginPost(HttpServletResponse httpServletResponse) {
        httpServletResponse.setHeader("Location", "/home");
    }

}

The solution was to add the controller I included in Edit 2 and I also had to edit the successfulAuthentication in the login filter to the following: 解决的办法是增加我列入编辑2控制器和我也有编辑successfulAuthentication在登录过滤器为以下内容:

@Override
protected void successfulAuthentication(HttpServletRequest req,
        HttpServletResponse res, FilterChain chain, Authentication auth) {
    TokenAuthenticationService.addAuthentication(res, auth.getName());
    chain.doFilter(req, res);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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