z3r*_*3r9 2 spring-security jsf-2
对不起我的英语不好。为什么isAuthenticated()在Spring Security 中没有工作方法?我在JSF中使用:
#{loginMB.authentication.authenticated}
<sec:authorize access="hasRole('ROLE_ADMIN')">
test
</sec:authorize>
Run Code Online (Sandbox Code Playgroud)
它不起作用。无论true我是否通过验证,它始终都会返回。
如果显示角色:
#{loginMB.authentication.authorities}
Run Code Online (Sandbox Code Playgroud)
如图所示,经过身份验证的角色是[ROLE_ADMIN],未经身份验证的角色是[ROLE_ANONYMOUS]。
什么时候有问题?
====更新====
如果创建梅托德isAuthenticated()在LoginBean办理登机手续AnonymousAuthenticationToken的亚历山大说:
public boolean isAuthenticated(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();
}
Run Code Online (Sandbox Code Playgroud)
这是工作。谢谢Aleksandr。但是授权标签不起作用。如果我在JSF页面中添加:
<sec:authorize access="hasRole('ROLE_ANONYMOUS')">
ROLE_ANONYMOUS
</sec:authorize>
<sec:authorize access="hasRole('ROLE_ADMIN')">
ROLE_ADMIN
</sec:authorize>
Run Code Online (Sandbox Code Playgroud)
它打印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>
Run Code Online (Sandbox Code Playgroud)
问题解决了。
如果在LoginBean中创建方法isAuthenticated()来检查AnonymousAuthenticationToken,如Aleksandr所述:
public boolean isAuthenticated(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();
}
Run Code Online (Sandbox Code Playgroud)
这是工作。谢谢Aleksandr。
| 归档时间: |
|
| 查看次数: |
5823 次 |
| 最近记录: |