Java 第二节:授权不';不适用于新添加的用户
我已经完成了mkyong的springsecuritywithdatabases教程。一切正常,但现在,当我将新用户添加到用户和用户角色表,然后登录这些帐户时,sec:authorize withJava 第二节:授权不';不适用于新添加的用户,java,spring,spring-security,Java,Spring,Spring Security,我已经完成了mkyong的springsecuritywithdatabases教程。一切正常,但现在,当我将新用户添加到用户和用户角色表,然后登录这些帐户时,sec:authorize withaccess=“hasRole('ROLE\u user')”不起作用,并且sec:authorize标记中的任何数据都不会显示 有spring安全设置 <beans:beans xmlns="http://www.springframework.org/schema/security"
access=“hasRole('ROLE\u user')”
不起作用,并且sec:authorize标记中的任何数据都不会显示
有spring安全设置
<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"
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.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
<form-login
login-page="/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
<!-- Select users and user_roles from database -->
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"select username,password, enabled from users where username=?"
authorities-by-username-query=
"select username, role from user_roles where username =? " />
</authentication-provider>
</authentication-manager>
</beans:beans>
问题解决了
正如@holmis83所建议的,我已经补充了
<sec:authentication property="principal.authorities"/>
检查my db中用户角色表中角色列的内容。结果是,我将角色保存为“USER\u ROLE”,应该是“ROLE\u USER”。我已将用户角色更新为正确的值,现在一切正常。角色缓存在会话中,这是您的意思吗?不,我只是说当用户登录sec时:授权不起作用,并且该块的内容不显示@Holmis83如果打印角色,您会得到什么,例如,
?用户角色应为角色用户。谢谢
//this works
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetail = (UserDetails) auth.getPrincipal();
model.addObject("username", userDetail.getUsername());
}
<sec:authentication property="principal.authorities"/>