gpt4 book ai didi

jsf-2 - spring security 3 isAuthenticated() 不工作

转载 作者:行者123 更新时间:2023-12-02 04:49:00 28 4
gpt4 key购买 nike

对不起我的英语。为什么 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>

最佳答案

问题解决了。

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

       public boolean isAuthenticated(){

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

    }

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

  2. 将在 JSF 页面中授权标签读取 here .我有it问题。

关于jsf-2 - spring security 3 isAuthenticated() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19221979/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com