【问题标题】:spring security 3 isAuthenticated() not workingspring security 3 isAuthenticated() 不工作
【发布时间】:2013-10-13 20:35:01
【问题描述】:

对不起我的英语。为什么 Spring 安全中的 isAuthenticated() 方法不起作用?我在 JSF 中使用:

#{loginMB.authentication.authenticated}

<sec:authorize access="hasRole('ROLE_ADMIN')">
    test
</sec:authorize>

它不工作。无论我是否经过身份验证,它都会返回true

如果显示角色:

#{loginMB.authentication.authorities}

显示对了,认证时角色为[ROLE_ADMIN],未认证时角色为[ROLE_ANONYMOUS]

什么时候出问题?

==== 更新 ====

如果在LoginBean 中创建方法isAuthenticated() 以检查AnonymousAuthenticationToken,如Aleksandr 所说:

public boolean isAuthenticated(){

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();

}

它正在工作。谢谢亚历山大。但是授权标签不起作用。如果我在 JSF 页面中添加:

<sec:authorize access="hasRole('ROLE_ANONYMOUS')">
    ROLE_ANONYMOUS
</sec:authorize>
<sec:authorize access="hasRole('ROLE_ADMIN')">
    ROLE_ADMIN
</sec:authorize>

它打印 ROLE_ANONYMOUS 和 ROLE_ADMIN。为什么?

==== 更新 2 ====

applicationContext-security.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"
             xmlns:context="http://www.springframework.org/schema/context"
             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.1.xsd">

    <beans:import resource="applicationContext.xml"/>


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

    <http auto-config="true" use-expressions="true">
        <form-login login-page="/pages/login.html" authentication-failure-url="/fail.html"/>
        <intercept-url pattern="/**" access="permitAll" />

    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="UserDAO">
            <password-encoder hash="plaintext" />
        </authentication-provider>
    </authentication-manager>

</beans:beans>

【问题讨论】:

  • 它工作正常。如果你想使用这个方法,你只需要检查AnonymousAuthenticationToken
  • 我在 LoginBean 中创建了方法 isAuthenticated() 用于检查 AnonymousAuthenticationToken,它是有效的。但是spring security授权标签不起作用。
  • 我在“更新”部分的主帖中添加了我的意思。
  • access 属性接受表达式。
  • 如果你的意思是hasRole('ROLE_ADMIN') 它不工作。我编辑了主帖。

标签: jsf-2 spring-security


【解决方案1】:

问题解决了。

  1. 如果在 LoginBean 中创建方法 isAuthenticated() 以检查 AnonymousAuthenticationToken,如 Aleksandr 所说:

       public boolean isAuthenticated(){
    
           Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
           return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();
    
       }
    

    它正在工作。谢谢亚历山大。

  2. 对于将工作授权标记在 JSF 页面中读取 here。我遇到了it 问题。

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2011-09-19
    • 2016-05-30
    • 2023-02-04
    • 2018-07-13
    • 2017-04-14
    • 1970-01-01
    • 2019-09-27
    • 2016-05-20
    相关资源
    最近更新 更多