抱歉我的英文。为什么Spring安全性中没有工作方法isAuthenticated()
?我在JSF中使用:
#{loginMB.authentication.authenticated}
<sec:authorize access="hasRole('ROLE_ADMIN')">
test
</sec:authorize>
它不起作用。如果我进行了身份验证,那么它一直返回true
。
如果显示角色:
#{loginMB.authentication.authorities}
显示权限,经过身份验证时,角色为[ROLE_ADMIN]
,未经过身份验证时,角色为[ROLE_ANONYMOUS]
。
什么时候出问题?
====已更新====
如果在isAuthenticated()
中创建metod LoginBean
以检查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>
答案 0 :(得分:4)
问题解决了。
如果在LoginBean中创建metod isAuthenticated()以检查AnonymousAuthenticationToken为所述Aleksandr:
public boolean isAuthenticated(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();
}
它正在发挥作用。谢谢Aleksandr。