【问题标题】:403 - Access is denied after authenticating403 - 认证后访问被拒绝
【发布时间】:2010-11-19 17:28:51
【问题描述】:

您好!

当我尝试对我现有的数据库进行身份验证时,我正在通过身份验证,但我得到了 403 页面。如果我只是尝试了错误的密码,我会按预期收到“错误的凭据”消息。 我尝试对 SpringSecurity 中包含的每个示例应用程序进行身份验证,并且效果很好。

安全上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">

    <global-method-security secured-annotations="enabled"></global-method-security>

    <http auto-config="true" >
        <intercept-url pattern="/admin/**" access="ROLE_TEST" />
        <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />

        <form-login
            login-page="/login/index.jsp"
            default-target-url="/admin/test.jsp"
            authentication-failure-url="/login/index.jsp?login_error=1" />  
    </http>

    <authentication-provider user-service-ref="jdbcUserService">      
        <password-encoder ref="passwordEncoder">
                <salt-source system-wide="n103df"/>
        </password-encoder>        
    </authentication-provider>


    <beans:bean id="jdbcUserService"  class="org.springframework.security.userdetails.jdbc.JdbcDaoImpl">
        <beans:property name="rolePrefix" value="" />
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="enableAuthorities" value="true"/>
        <beans:property name="enableGroups" value="false"/>
        <beans:property name="authoritiesByUsernameQuery" value="SELECT username,authority FROM authorities WHERE username = ?" />
        <beans:property name="usersByUsernameQuery" value="SELECT username,password,enabled as enabled FROM users WHERE username = ?" />
        <beans:property name="groupAuthoritiesByUsernameQuery" value="" />

    </beans:bean>

<beans:bean id="passwordEncoder" class="org.springframework.security.providers.encoding.Md5PasswordEncoder"/>

将不胜感激任何帮助:-) 提前致谢!

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    如果您收到 403 代码,则表示用户没有所需的角色。所以,认证不是问题,是授权。
    知道发生了什么的唯一方法是将日志记录级别进行调试,应该有更多信息。在这里发布。
    您的角色是否有“ROLE_”前缀?

    【讨论】:

      【解决方案2】:

      ...想通了。尽管在配置文件中指定了 ROLE_TEST 并且在 db 的“权限”列中指定了相同,但 Spring-Sec 期待 ROLE_SUPERVISOR:

      [DEBUG,AbstractSecurityInterceptor,http-8084-7] 安全对象:FilterInvocation:URL:/admin/test.jsp;配置属性:[ROLE_TEST] [DEBUG,AbstractSecurityInterceptor,http-8084-7] 先前认证:org.springframework.security.providers.UsernamePasswordAuthenticationToken@af840ed7:主体:org.springframework.security.userdetails.User@3ec100:用户名:testUser;密码保护];启用:真; AccountNonExpired:真;凭据非过期:真; AccountNonLocked:真;授予权限:ROLE_SUPERVISOR;密码保护];已认证:真实;详细信息:org.springframework.security.ui.WebAuthenticationDetails@1c07a:RemoteIpAddress:127.0.0.1;会话ID:350B260FAFDDBF04D5CB4AAAB7B8A441;授予权限:ROLE_SUPERVISOR [DEBUG,ExceptionTranslationFilter,http-8084-7] 访问被拒绝(用户不是匿名的);委托给 AccessDeniedHandler org.springframework.security.AccessDeniedException:访问被拒绝 在 org.springframework.security.vote.AffirmativeBased.decide(AffirmativeBased.java:68)

      ...现在我很好奇,怎么会? 因此,在配置文件中将 ROLE_TEST 更改为 ROLE_SUPERVISOR 后,一切都按预期工作。

      【讨论】:

      • ....我今天意识到,我一直在看错误的数据库,'不用说了。不过,这确实有帮助!
      猜你喜欢
      • 2015-09-07
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2017-03-16
      相关资源
      最近更新 更多