繁体   English   中英

Spring Security +记住我的支持+自动重定向

[英]Spring Security + Remember me support + auto redirect

我有一个spring + hibernate项目,该项目使用spring安全性进行身份验证,并且一切工作都像一个魅力。 我在下面有spring-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"  
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/security  
http://www.springframework.org/schema/security/spring-security-3.2.xsd">  

<!-- enable use-expressions -->  
<http auto-config="true" use-expressions="true"> 
 <intercept-url pattern="/home" access="isAuthenticated()" />
 <intercept-url pattern="/home/**" access="isAuthenticated()" /> 


  <form-login 
    login-page="/" 
    authentication-failure-url="/?error"  
    username-parameter="username" 
    password-parameter="password" 
    default-target-url="/home" />  

   <!-- access denied page -->  
   <access-denied-handler error-page="/403" />
   <!-- logout handling -->
   <logout invalidate-session="true" logout-success-url="/?logout" delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE" />  
   <!-- enable csrf protection  <csrf />  -->  
   <remember-me services-ref="rememberMeServices" key="clarkerpi" />

  </http>  

  <beans:bean id="rememberMeServices"  class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
    <beans:property name="tokenRepository" ref="customTokenRepository" />
    <beans:property name="userDetailsService" ref="userDetailsService" />
    <beans:property name="key" value="clarkerpi" />
 </beans:bean> 

 <authentication-manager alias="authenticationManager">
    <authentication-provider ref="authenticationProvider" />  
 </authentication-manager>   

 <beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
 </beans:bean>


</beans:beans>  

几乎一切正常。 我可以登录并选中“记住我”,它会创建Cookie,保留令牌以及所有其他内容。 如果我删除JSESSIONID cookie,我仍然可以访问受保护的资源。

但是我有一个问题

如果我访问localhost / projectname /,并且“ /”是我的登录页面,是否有任何本机方式(春季安全性)重定向到target-url,对于具有Remember_me Cookie的用户而言,该地址是/ home? 我可以毫无问题地访问任何受保护的资源,但是我想输入localhost / projectname /并访问/ home。 当然,让登录页面可用于非记住我登录。

问题2)我对Spring Security + Cookie处理非常陌生,可以像在注销时一样删除JSESSIONID和Remember_me Cookie吗? 要么?

在此先感谢,// fferrandini

我找到了解决方案。

弹簧安全性可以完美地发挥作用。 当您具有“ remember_me”活动并访问受保护的资源时,它的确可以发挥作用,而这就是他的全部工作。 春季安全性确实照顾了重定向或“认证后我该怎么办”的问题...

解决方案非常简单。

我在mkyong找到了这种方法:

private boolean isRememberMeAuthenticated() {

    Authentication authentication =          SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
       return false;
    }

    return RememberMeAuthenticationToken.class.isAssignableFrom(authentication.getClass());
 }

您可以在控制器中甚至在过滤器中进行设置。 我在登录映射...“ /”中设置了它。 如果为true,则重定向到/ home,否则请进行登录。

希望对别人有帮助!

春天你很漂亮! 大声笑

像这样“ / login”和<intercept-url pattern="/login" access="permitAll()"/>更改登录页面,并将“ /”映射到“ / home”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM