使用Spring 3在REST中登录/注销
我们正在使用Spring 3开发RESTful Web服务,我们需要具有登录和注销功能,例如/webservices/login/<username>/<password>/和/webservices/logout。会话应存储在上下文中,直到会话超时或注销以允许使用其他Web服务。没有会话信息的任何访问Web服务的请求都应被拒绝。寻找这种情况的最新解决方案。
我实际上是在复活这里Spring Security 3以编程方式登录的问题,仍然无法正确回答。请同时在web.xml中指定所需的更改。
回答:
我建议完全手动定义你的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" class="org.springframework.security.web.FilterChainProxy"> <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" class="org.springframework.security.web.context.SecurityContextPersistenceFilter"/>现在,链中的下一个过滤器可以将数据添加到安全性上下文(读取:登录/注销用户),或基于所述安全性上下文来决定是否允许访问。
对于你的登录URL,你将需要一个过滤器,该过滤器从请求中读取身份验证数据,对其进行验证,然后将其存储在安全上下文中(存储在会话中):
<bean id="wsFilter" class="my.own.security.AuthenticationFilter"> <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 {@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
return ( StringUtils.hasText(obtainUsername(request)) && StringUtils.hasText(obtainPassword(request)) );
}
@Override
protected 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" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<property name="preAuthenticatedUserDetailsService">
<bean id="userDetailsServiceWrapper" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<property name="userDetailsService" ref="myUserDetailsImpl"/>
</bean>
</property>
</bean>
你将必须在此处提供你自己的用户详细信息Bean实现。
注销过滤器:负责清除安全上下文
<bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter"> <constructor-arg>
<list>
<bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
</list>
</constructor-arg>
</bean>
通用身份验证内容:
<bean id="httpRequestAccessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased"> <property name="allowIfAllAbstainDecisions" value="false"/>
<property name="decisionVoters">
<list>
<ref bean="roleVoter"/>
</list>
</property>
</bean>
<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"/>
<bean id="securityContextHolderAwareRequestFilter" class="org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter"/>
访问控制过滤器(应该不言自明):
<bean id="fsi" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> <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标记,也可以至少执行这里实现的大部分操作,但是我更喜欢自定义方法,因为它可以给我最大的控制权。
以上是 使用Spring 3在REST中登录/注销 的全部内容, 来源链接: utcz.com/qa/402023.html

