【发布时间】:2017-10-19 10:32:38
【问题描述】:
我已经使用带有 mongo 数据库的 generator-jhipster 创建了一个项目。
我想在 JHI_AUTHORITY 中添加一个新角色,所以我在 mongo {"_id" : "ROLE_MANAGER"} 的 JHI_AUTHORITY 文档中插入了一条新记录
现在我希望只有ROLE_ADMIN 或ROLE_MANAGER 的用户才有权使用其中一个api。
所以我在我的 api 中添加了以下 LOC:
@secured({"ROLE_ADMIN", "ROLE_MANAGER"})
但是当我尝试使用具有角色的用户访问 api 时:ROLE_ADMIN 效果很好,但是当用户具有角色: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();
}
}
【问题讨论】:
-
请显示您的安全配置类
-
@GaëlMarziou 我在我的问题中添加了安全配置类。请查看更新。
标签: mongodb spring-boot jhipster