栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过Spring的RESTful身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Spring的RESTful身份验证

我们设法完全按照OP中的描述进行工作,希望其他人可以使用该解决方案。这是我们所做的:

像这样设置安全上下文:

<security:http realm="Protected API" use-expressions="true" auto-config="false" create-session="stateless" entry-point-ref="CustomAuthenticationEntryPoint">    <security:custom-filter ref="authenticationTokenProcessingFilter" position="FORM_LOGIN_FILTER" />    <security:intercept-url pattern="/authenticate" access="permitAll"/>    <security:intercept-url pattern="/**" access="isAuthenticated()" /></security:http><bean id="CustomAuthenticationEntryPoint"     /><bean id="authenticationTokenProcessingFilter"     >    <constructor-arg ref="authenticationManager" /></bean>

如你所见,我们创建了一个自定义AuthenticationEntryPoint,401 Unauthorized如果我们的请求未在过滤器链中进行身份验证,则该自定义基本上只会返回一个AuthenticationTokenProcessingFilter。

CustomAuthenticationEntryPoint:public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {    @Override    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {        response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized: Authentication token was either missing or invalid." );    }}

AuthenticationTokenProcessingFilter:

public class AuthenticationTokenProcessingFilter extends GenericFilterBean {    @Autowired UserService userService;    @Autowired TokenUtils tokenUtils;    AuthenticationManager authManager;    public AuthenticationTokenProcessingFilter(AuthenticationManager authManager) {        this.authManager = authManager;    }    @Override    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        @SuppressWarnings("unchecked")        Map<String, String[]> parms = request.getParameterMap();        if(parms.containsKey("token")) { String token = parms.get("token")[0]; // grab the first "token" parameter // validate the token if (tokenUtils.validate(token)) {     // determine the user based on the (already validated) token     UserDetails userDetails = tokenUtils.getUserFromToken(token);     // build an Authentication object with the user's info     UsernamePasswordAuthenticationToken authentication =   new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword());     authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails((HttpServletRequest) request));     // set the authentication into the SecurityContext     SecurityContextHolder.getContext().setAuthentication(authManager.authenticate(authentication));          }        }        // continue thru the filter chain        chain.doFilter(request, response);    }}

显然,TokenUtils其中包含一些私有(且非常与案例有关)的代码,并且不能轻易共享。这是它的界面:

public interface TokenUtils {    String getToken(UserDetails userDetails);    String getToken(UserDetails userDetails, Long expiration);    boolean validate(String token);    UserDetails getUserFromToken(String token);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432097.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号