【发布时间】:2018-04-03 12:36:55
【问题描述】:
早安,
我正在使用spring security来限制用户,并且我正在使用mongodb。
我已经创建了 UserDetail 和 userDetail 服务。
她是我的 webSecurity 配置。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService mongoUserDetails() {
return new CustomUserDetailsService();
}
@Bean
public DaoAuthenticationProvider authProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(mongoUserDetails());
authProvider.setPasswordEncoder(new BCryptPasswordEncoder());
return authProvider;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home", "/static/unsecure/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
我正在像这样在数据库中存储权限。
"permissions" : [
"/reports/report1",
"/reports/report2"
]
这里是 userDetail 服务。
public class MongoUserDetails implements UserDetails {
private final String username;
private final String password;
private final List<GrantedAuthority> grantedAuthorities;
private final boolean enabled;
public MongoUserDetails(String username, String password, String[] authorities, boolean isEnabled) {
this.username = username;
this.password = password;
this.grantedAuthorities = AuthorityUtils.createAuthorityList(authorities);
this.enabled = isEnabled;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return grantedAuthorities;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
}
在私有最终列表中被授予权威;我已成功存储权限。分别是“/reports/report1”、“/reports/report2”
当我运行我的网络应用程序时,它会将我重定向到登录页面,并在获得有效凭据后将我重定向到 /hello 页面。
现在我正在创建控制器和动作,例如
@Controller
@ResponseBody
public class MyController {
@GetMapping("/user/greeting")
public String greeting(HttpServletRequest request){
return "welcome secure";
}
@GetMapping("/login1")
public String greeting1(HttpServletRequest request){
}
}
所以自从列出grantAuthorities;不包含此路由,因此不应允许用户执行此操作
我怎样才能做到这一点。假设所有权限都是动态的。
谢谢。
【问题讨论】:
标签: java spring spring-mvc spring-boot spring-security