Java Spring安全+记住我支持+自动重定向

Java Spring安全+记住我支持+自动重定向,java,cookies,spring-security,Java,Cookies,Spring Security,我得到了一个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/XMLSc

我得到了一个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>  
几乎所有的东西都能用。我可以登录并检查RememberMe,它会创建cookies、持久化令牌等等。如果我删除JSESSIONID cookie,我仍然可以访问受保护的资源

但我有个问题

如果我访问localhost/projectname/,并且/作为我的登录页面,是否有任何本机方式的spring安全性可以重定向到目标url,对于使用Memory\u me cookie的人来说,目标url是/home?我可以毫无问题地访问任何受保护的资源,但我想键入localhost/projectname/和access/home。当然,让登录页面可用于非RememberMe登录

问题2我对spring security+cookie处理非常陌生,是否可以删除JSESSIONID并像注销时一样记住cookie?还是

提前感谢,, //费兰迪尼我找到了解决办法

spring security完美地完成了它的工作。当你记得我是活跃的,并且访问受保护的资源时,它确实像一个符咒一样工作,这就是他的全部工作。Spring security不负责重定向,也不负责身份验证后的重定向

解决办法很简单

我在mkyong找到了这种方法:

private boolean isRememberMeAuthenticated() {

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

    return RememberMeAuthenticationToken.class.isAssignableFrom(authentication.getClass());
 }
您可以在控制器或更好的过滤器中设置它。我在登录映射中设置了它…/。如果为true,则重定向到/home,否则允许登录

希望这能帮助别人


春天你真漂亮!!lol

像这样更改您的登录页面/login和 和地图/到/回家