Spring security always returns HTTP 403

前端 未结 7 2077
逝去的感伤
逝去的感伤 2020-12-28 17:07

I have configured a custom Filter that grants a spring authority for every URL other than /login :

public class TokenFilter impleme         


        
相关标签:
7条回答
  • 2020-12-28 18:08

    UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken, AbstractAuthenticationToken implements Authentication.

    Spring security call Authentication's method isAuthenticated() to check whether it should be pass.

    So you should call setAuthenticated of UsernamePasswordAuthenticationToken instance and set the argument true.

    Like this:

    public class TokenFilter implements Filter {
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            GrantedAuthority authority = new SimpleGrantedAuthority("myAuthority");
            UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, token, Arrays.asList(authority));
    
            auth.setAuthenticated(true);
    
            SecurityContextHolder.getContext().setAuthentication(auth);
      }
    }
    
    0 讨论(0)
提交回复
热议问题