具有hibernate:hasRole()的Spring安全性在配置中不起作用
我正在尝试为我的Spring+Hibernate项目实现Spring安全性 但是我在intercept url标记中编写的hasRole('SUPER_ADMIN')不起作用 请在下面找到我所做的配置 springSecurity.xml具有hibernate:hasRole()的Spring安全性在配置中不起作用,spring,hibernate,spring-mvc,spring-security,Spring,Hibernate,Spring Mvc,Spring Security,我正在尝试为我的Spring+Hibernate项目实现Spring安全性 但是我在intercept url标记中编写的hasRole('SUPER_ADMIN')不起作用 请在下面找到我所做的配置 springSecurity.xml <http auto-config="true" use-expressions="true"> <intercept-url pattern="/admin**" access="hasRole('SUPER_ADMIN')
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('SUPER_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"
login-processing-url="/loginCheck" password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf />
</http>
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsServices">
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
. 用户正在获得身份验证并登录。但是上面的url被重定向到/403(拒绝访问)
我错过什么了吗?请帮忙 hasRole
运行良好。代码中不起作用的是springSecurity.xml上的通配符。
改变这个
到
不确定原因和方式,但spring似乎添加了一个前导斜杠来验证您的url
因此,拥有
/admin/**
将具有与您预期的/admin**
相同的效果。通过在角色名中添加“ROLE_u”,可以纠正这一问题。作为超级管理员,它开始工作。我假设每个角色都应该加上“role_3;”前缀,这样SpringSecurity才能正常工作
谢谢@storm\u buster的提示。:) 嗨@storm_blaster,我也试过,但不幸的是,它也不起作用(它应该可以工作。看,要排除是模式导致了错误,您可以将访问方法更改为isAuthenticated()例如,如果你没有得到403,这意味着你的角色有问题。可能正在检查角色超级管理员。非常感谢你的帮助。在超级管理员前面添加一个“角色”是有效的。我假设每个春季角色都应该以“角色”开头。我不认为这是强制性的。所以从未尝试过该选项。:)有一个设置,设置那个前缀。记不清是哪一个了,得在文件里找。但我确实在那里读到了。如果斯托姆·巴斯特帮你解决了这个问题,那么投票或接受(勾选)他或她的答案是一个好的姿态吗?
public class MyUserDetailsServices implements UserDetailsService {
private UserDao userDao;
@Override
@Transactional
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
User user = userDao.findByEmail(username);
if (user == null) {
throw new UsernameNotFoundException("User " + username + " not found");
}
List<GrantedAuthority> authorities = buildUserAuthority(user.getRoles());
return buildUserForAuthentication(user, authorities);
}
private org.springframework.security.core.userdetails.User buildUserForAuthentication(User user, List<GrantedAuthority> authorities) {
return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), true, true, true, true, authorities);
}
private List<GrantedAuthority> buildUserAuthority(Set<Role> userRoles) {
Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
for (Role userRole : userRoles) {
setAuths.add(new SimpleGrantedAuthority(userRole.getRoleName()));
}
List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);
return Result;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}