简体   繁体   English

Spring Security未授权用户

[英]Spring security not authorizing user

I'm new to Spring Security and I'm developing a web app which requires authentication and authorization using Spring Security 3.2, the authentication part is working fine but the authorization is not. 我是Spring Security的新手,我正在开发一个Web应用程序,该应用程序需要使用Spring Security 3.2进行身份验证和授权,身份验证部分工作正常,但授权无效。 Below is my spring security configuration xml snippet. 以下是我的spring安全配置xml片段。

<authentication-manager>
    <authentication-provider>
        <password-encoder ref="encoder" />
        <jdbc-user-service data-source-ref="myDataSource"
            users-by-username-query=" SELECT email_address as username , password, enabled FROM users WHERE email_address = ?   "
            authorities-by-username-query=" SELECT u.email_address as username , 
                                            r.role_name FROM users u
                                            INNER JOIN user_roles ur
                                            ON ur.user_id = u.user_id
                                            INNER JOIN roles r
                                            ON r.role_id = ur.role_id
                                            WHERE u.email_address = ? "/>
    </authentication-provider>
</authentication-manager>

<beans:bean id="encoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <beans:constructor-arg name="strength" value="11" />
</beans:bean>

<http pattern="/resources/**" security="none" />

<http auto-config="true" use-expressions="true" create-session="ifRequired">

    <form-login login-page="/" default-target-url="/admin/dashboard"
        authentication-failure-url="/login-error" always-use-default-target="true" />

    <!-- Security zones -->
    <intercept-url pattern="/" access="isAnonymous()" />
    <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" />

    <session-management invalid-session-url="/"
        session-fixation-protection="newSession">
        <concurrency-control max-sessions="1"
            error-if-maximum-exceeded="true" />
    </session-management>

    <logout logout-success-url="/" delete-cookies="JSESSIONID"
        invalidate-session="true" />

    <access-denied-handler error-page="/403" />
</http>

With this configuration everything works fine apart from authorization. 使用此配置,除了授权之外,一切都可以正常工作。 I have two users viz tim@abc.com (role=ADMIN) and bob@abc.com(role=USER) , but when I try to login with bob@abc.com then also I'm able to view the admin/dashboard page which should not happen. 我有两个用户,即tim@abc.com(role = ADMIN)bob@abc.com(role = USER) ,但是当我尝试使用bob@abc.com登录时,我也可以查看admin /仪表板页面,这不应发生。

I've referred many tutorials and spring doc as well but not able to find the exact problem. 我也参考了许多教程和Spring文档,但无法找到确切的问题。 Please help. 请帮忙。

Change the pattern to "/admin/*" 将模式更改为“ / admin / *”

<intercept-url access="hasRole('ROLE_ADMIN')" />

Your default-target-url="/admin/dashboard" seems to be confusing as for every user it will redirect to /admin/dashboard after login. 您的default-target-url =“ / admin / dashboard”似乎令人困惑,因为每个用户登录后都会重定向到/ admin / dashboard。 You may get http UnAuthorized response when you login with bob@abc.com. 使用bob@abc.com登录时,您可能会收到http未经授权的响应。

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

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