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

使用Spring 3在REST中登录/注销

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

使用Spring 3在REST中登录/注销

我建议完全手动定义你的Spring Security过滤器。这并不困难,你可以完全控制登录/注销行为。

首先,你将需要标准的web.xml blurb来将过滤器链处理委托给Spring(如果你不使用Servlet API版本3,请删除异步支持):

<filter>    <filter-name>springSecurityFilterChain</filter-name>    <async-supported>true</async-supported>    <filter-class>        org.springframework.web.filter.DelegatingFilterProxy    </filter-class></filter><filter-mapping>    <filter-name>springSecurityFilterChain</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

现在,在安全性上下文中,你将为每个路径分别定义过滤器。筛选器可以验证用户身份,注销用户,检查安全凭证等。

<bean id="springSecurityFilterChain" >    <sec:filter-chain-map path-type="ant">        <sec:filter-chain pattern="/login" filters="sif,wsFilter"/>        <sec:filter-chain pattern="/logout" filters="sif,logoutFilter" />        <sec:filter-chain pattern="/rest/**" filters="sif,fsi"/>    </sec:filter-chain-map></bean>

上面的XML告诉Spring通过过滤器链将请求传递到特定于上下文的URL。任何过滤器链中的第一件事就是建立安全性上下文-“ sif” bean负责这一点。

<bean id="sif" />

现在,链中的下一个过滤器可以将数据添加到安全性上下文(读取:登录/注销用户),或基于所述安全性上下文来决定是否允许访问。

对于你的登录URL,你将需要一个过滤器,该过滤器从请求中读取身份验证数据,对其进行验证,然后将其存储在安全上下文中(存储在会话中):

<bean id="wsFilter" >  <property name="authenticationManager" ref="authenticationManager"/>  <property name="authenticationSuccessHandler" ref="myAuthSuccessHandler"/>  <property name="passwordParameter" value="pass"></property>  <property name="usernameParameter" value="user"></property>  <property name="postOnly" value="false"></property>

你可以使用Spring泛型,

UsernamePasswordAuthenticationFilter
但我使用自己的实现的原因是继续进行过滤器链处理(默认实现假定用户将在成功的身份验证后重定向,并终止过滤器链),并且每次将用户名和密码传递给用户时都能够处理身份验证它:

public class MyAuthenticationFilter extends UsernamePasswordAuthenticationFilter {@Overrideprotected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {    return ( StringUtils.hasText(obtainUsername(request)) && StringUtils.hasText(obtainPassword(request)) );}@Overrideprotected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,        Authentication authResult) throws IOException, ServletException{    super.successfulAuthentication(request, response, chain, authResult);    chain.doFilter(request, response);}

你可以为/ login路径添加任意数量的过滤器实现,例如使用HTTP基本auth标头,摘要标头进行身份验证,甚至从请求主体中提取用户名/密码。Spring为此提供了一堆过滤器。

我有自己的身份验证成功处理程序,该处理程序将覆盖默认的重定向策略:

public class AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {   @PostConstruct   public void afterPropertiesSet() {       setRedirectStrategy(new NoRedirectStrategy());   }    protected class NoRedirectStrategy implements RedirectStrategy {        @Override        public void sendRedirect(HttpServletRequest request,     HttpServletResponse response, String url) throws IOException { // no redirect        }    }}

如果你可以成功登录后将用户重定向(你可以自定义重定向URL,请检查文档),就可以不必自定义身份验证成功处理程序(也可能还需要自定义身份验证过滤器)

定义身份验证管理员,该管理员将负责检索用户的详细信息:

<sec:authentication-manager alias="authenticationManager">    <sec:authentication-provider ref="myAuthAuthProvider"/></sec:authentication-manager> <bean id="myAuthAuthProvider" >    <property name="preAuthenticatedUserDetailsService">        <bean id="userDetailsServiceWrapper" > <property name="userDetailsService" ref="myUserDetailsImpl"/>        </bean>    </property></bean>

你将必须在此处提供你自己的用户详细信息Bean实现。

注销过滤器:负责清除安全上下文

<bean id="logoutFilter" >    <constructor-arg>        <list> <bean />        </list>    </constructor-arg></bean>

通用身份验证内容:

<bean id="httpRequestAccessDecisionManager" >    <property name="allowIfAllAbstainDecisions" value="false"/>    <property name="decisionVoters">        <list> <ref bean="roleVoter"/>        </list>    </property></bean><bean id="roleVoter" /><bean id="securityContextHolderAwareRequestFilter" />

访问控制过滤器(应该不言自明):

<bean id="fsi" >    <property name="authenticationManager" ref="myAuthenticationManager"/>    <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>    <property name="securitymetadataSource">        <sec:filter-invocation-definition-source> <sec:intercept-url pattern="/rest/**" access="ROLE_REST"/>        </sec:filter-invocation-definition-source>    </property></bean>

你还应该能够@Secured通过方法的注释来保护REST服务。

上面的上下文是从现有的REST服务webapp中摘录的,对于任何可能的错字表示抱歉。

通过使用常规secSpring标记,也可以至少执行这里实现的大部分操作,但是我更喜欢自定义方法,因为它可以给我最大的控制权。



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

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

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