Spring Security Token based Authentication

后端 未结 2 502
误落风尘
误落风尘 2020-12-04 09:22

I have a rest api where I am authenticating using spring security Basic Authorization where client sends username and password for each request. Now, I wanted to implement t

2条回答
  •  我在风中等你
    2020-12-04 09:50

    You can try setting your custom AuthenticationToken token in your authentication filter, for example:

    public class AuthenticationFilter extends GenericFilterBean {
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            final String authTokenHeader = ((HttpServletRequest)request).getHeader(Constants.AUTH_HEADER_NAME);
    
            if (authTokenHeader != null) {
                SecurityContextHolder.getContext().setAuthentication(createAuthenticationToken(authTokenHeader));
            }
    
            chain.doFilter( request, response );
        }
    }
    

提交回复
热议问题