我使用带有mongo数据库的generator-jhipster创建了一个项目。
我想在JHI_AUTHORITY
中添加一个新角色,因此我在mongo JHI_AUTHORITY
{"_id" : "ROLE_MANAGER"}
文档中插入了一条新记录
现在我希望只有ROLE_ADMIN
或ROLE_MANAGER
的用户才有权使用其中一个API。
所以我在我的api中添加了以下LOC:
@secured({"ROLE_ADMIN", "ROLE_MANAGER"})
但是,当我尝试使用具有角色ROLE_ADMIN
的用户访问api时效果很好,但是如果用户有角色:ROLE_MANAGER
,则会显示错误:
{ "error": "access_denied", "error_description": "Access is denied" }
如果有任何缺失的步骤,请告诉我?
安全配置类如下:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Inject
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/scripts/**/*.{js,html}")
.antMatchers("/bower_components/**")
.antMatchers("/i18n/**")
.antMatchers("/assets/**")
.antMatchers("/swagger-ui.html")
.antMatchers("/api/register")
.antMatchers("/api/activate")
.antMatchers("/api/account/reset_password/init")
.antMatchers("/api/account/reset_password/finish")
.antMatchers("/test/**");
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
return new SecurityEvaluationContextExtension();
}
}
答案 0 :(得分:0)
Spring安全角色具有默认前缀 - ROLE_
在您的数据库中,当您在ROLE_NAME
,@Secured
等中使用角色时,您需要将角色保存为@PreAuthorize
,而不使用前缀(例如NAME
)。