代码之家  ›  专栏  ›  技术社区  ›  Ta Sas

简单的春天,请帮助我

  •  1
  • Ta Sas  · 技术社区  · 16 年前

    我只想简单的记住我。我读过 http://static.springsource.org/spring-security/site/docs/3.0.x/reference/remember-me.html

    到目前为止我所做的:

    1. 创造了我自己的 UserDetailsService 使用Hibernate/JPA。 我的请求。不考虑任何记得我的东西
    2. 通过appContext考虑的配置 <security:remember-me key="89dqj219dn910lsAc12" user-service-ref="jpaUserDetailsService" token-validity-seconds="864000"/>
    3. 检查过了,饼干 SPRING_SECURITY_REMEMBER_ME_COOKIE 已经准备好了
    4. 登录到安全站点并运行
    5. 当我重新启动浏览器时,我不断收到一个错误:

      org.springframework.security.access.AccessDeniedException:访问被拒绝 身份验证对象作为字符串:org.springframework.security.Authentication.RememberMeAuthenticationToken@9ab72a70:Principal:de.myapp.businessobjects.AppUser@61f68b18:Username:myad;Password:[PROTECTED];Enabled:true;AccountNonExpired:true;credentialsnexpired:true;AccountNonLocked:true;PersonalInformation:65537;凭据:[受保护];已验证:true;详细信息:org.springframework.security.web.authentication.web authentication Details@957e:RemoteIpAddress:127.0.0.1;SessionId:null;授予的权限:ROLE_ADMIN,ROLE_USER

    下面是我的secContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:security="http://www.springframework.org/schema/security"
           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.0.xsd">
    
        <security:global-method-security pre-post-annotations="enabled">
        </security:global-method-security>
    
        <security:http use-expressions="true" access-denied-page="/accessDenied">
            <security:form-login
                    login-page="/login"
                    login-processing-url="/loginProcess"
                    default-target-url="/intro"
                    authentication-failure-url="/login?login_error=1"
                    />
            <security:logout
                    logout-url="/logout"
                    logout-success-url="/logoutSuccess"/>
    
            <security:intercept-url pattern="/**" access="permitAll"/>
            <security:intercept-url pattern="/login" access="permitAll"/>
            <security:intercept-url pattern="/styles/**" access="permitAll"/>
            <security:intercept-url pattern="/scripts/**" access="permitAll"/>
            <security:remember-me key="89dqj219dn910lsAc12" user-service-ref="jpaUserDetailsService"
                                  token-validity-seconds="864000"/>
        </security:http>
    
        <security:authentication-manager alias="authenticationManager">
            <security:authentication-provider user-service-ref="jpaUserDetailsService">
                <security:password-encoder hash="sha">
                </security:password-encoder>
            </security:authentication-provider>
        </security:authentication-manager>
    
        <bean id="rememberMeFilter" class=
                "org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
            <property name="rememberMeServices" ref="rememberMeServices"/>
            <property name="authenticationManager" ref="authenticationManager"/>
        </bean>
    
        <bean id="rememberMeServices" class=
                "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
            <property name="userDetailsService" ref="jpaUserDetailsService"/>
            <property name="key" value="89dqj219dn910lsAc12"/>
        </bean>
    
        <bean id="rememberMeAuthenticationProvider" class=
                "org.springframework.security.authentication.RememberMeAuthenticationProvider">
            <property name="key" value="89dqj219dn910lsAc12"/>
        </bean>
    </beans>
    

    最后是一些调试跟踪

    03:45:14.598 [7225609@qtp-10131947-7] DEBUG o.s.w.b.a.s.HandlerMethodInvoker - Invoking request handler method: public java.lang.String de.myapp.controller.bstController.showbstpage(java.lang.String,javax.servlet.http.HttpServletResponse)
    03:45:14.598 [7225609@qtp-10131947-7] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Secure object: ReflectiveMethodInvocation: public java.lang.String de.myapp.controller.bstController.showbstpage(java.lang.String,javax.servlet.http.HttpServletResponse); target is of class [de.myapp.controller.bstController]; Attributes: [[authorize: 'isFullyAuthenticated() and #username == principal.username', filter: 'null', filterTarget: 'null']]
    03:45:14.598 [7225609@qtp-10131947-7] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.RememberMeAuthenticationToken@9ab72a70: Principal: de.myapp.businessobjects.AppUser@61f68b18: Username: myad; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; PersonalInformation: 65537; ; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ADMIN, ROLE_USER
    03:45:14.599 [7225609@qtp-10131947-7] DEBUG o.s.c.c.s.GenericConversionService - Converting value false of [TypeDescriptor java.lang.Boolean] to [TypeDescriptor java.lang.Boolean]
    03:45:14.599 [7225609@qtp-10131947-7] TRACE o.s.c.c.s.GenericConversionService - Matched cached converter NO_OP
    03:45:14.599 [7225609@qtp-10131947-7] DEBUG o.s.c.c.s.GenericConversionService - Converted to false
    03:45:14.599 [7225609@qtp-10131947-7] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter@a866a9, returned: -1
    03:45:14.599 [7225609@qtp-10131947-7] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.RoleVoter@1ebf305, returned: 0
    03:45:14.599 [7225609@qtp-10131947-7] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.AuthenticatedVoter@19ffd6f, returned: 0
    

    我真的不知道在哪里继续调试。我错过了什么?我需要创建自己的“记住我”实现吗?

    我会的 真正地 感谢一个工作示例应用程序,它演示了SpringRememberMe的默认实现。。。

    --------编辑-----------

    我刚刚编译并运行了springsecurity自己的remember me reference应用程序: spring-security\samples\tutorial 帐户应用程序和联系人应用程序。实际上,我有 完全相同的问题 ?!?。我试过火狐、opera和ie。。。我崩溃了。。。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Shaun the Sheep    14 年前

    看起来在您的应用程序中,remember me身份验证工作正常,因为您从remember me cookie获得了有效的身份验证令牌。

    但是,日志输出指示在控制器方法上有一个方法访问控制注释 bstController.showbstpage 它需要“完全”身份验证,从表达式 isFullyAuthenticated() and #username == principal.username . 请记住,我不符合完全身份验证的条件,因此表达式拒绝当前身份验证。

    作为旁白 intercept-url 元素顺序错误,因为 /** 位于顶部,将应用于所有请求,使其他请求多余。

    另外,示例应用程序也不可能出现相同的问题,因为它们不需要对任何操作进行完全身份验证,所以您一定有其他问题。

        2
  •  0
  •   Doug Ayers    15 年前

    登录时,UserDetails对象上的“password”字段是否设置为非空值?在我的应用程序中,实际的身份验证被委托给另一个系统,并且我不会将用户提交的密码存储在我的UserDetails对象上。在将password属性设置为值之前,我无法使RememberMe cookie正常工作。在我的例子中,我只是将属性默认为单词“password”,这样就不会是空字符串。

    我不知道这是否和你想的一样,但这让我抓狂,直到我明白了。

    推荐文章