Spring boot 如何分别为管理员和用户配置spring boot的安全性
我正在尝试使用基于jwt令牌的身份验证保护演示应用程序。我的用例是我有两种类型的用户,一种是教师,是管理员,另一种是学生,是普通用户 我想分别为教师和学生配置安全性。我有不同的用户身份验证来源——一个用于教师,另一个用于普通用户。如果已配置两个WebSeconFigAdapter 在学生网络配置中Spring boot 如何分别为管理员和用户配置spring boot的安全性,spring-boot,spring-security,Spring Boot,Spring Security,我正在尝试使用基于jwt令牌的身份验证保护演示应用程序。我的用例是我有两种类型的用户,一种是教师,是管理员,另一种是学生,是普通用户 我想分别为教师和学生配置安全性。我有不同的用户身份验证来源——一个用于教师,另一个用于普通用户。如果已配置两个WebSeconFigAdapter 在学生网络配置中 config() { http.antMatcher("/student/getmarks/{id}") .authorizeRequests
config() {
http.antMatcher("/student/getmarks/{id}")
.authorizeRequests().anyRequest().hasRole("USER")
}
@Bean("studentAuth")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
config() {
http.antMatcher("/student/**","/teacher/**")
.authorizeRequests().anyRequest().hasRole("ADMIN")
}
@Bean("teacherAuth")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
在教师网络配置中
config() {
http.antMatcher("/student/getmarks/{id}")
.authorizeRequests().anyRequest().hasRole("USER")
}
@Bean("studentAuth")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
config() {
http.antMatcher("/student/**","/teacher/**")
.authorizeRequests().anyRequest().hasRole("ADMIN")
}
@Bean("teacherAuth")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
如何为两者配置单独的身份验证管理器。我尝试用不同的名称创建单独的bean。它给出的错误是身份验证管理器应该只有一个
希望只为类型接口org.springframework.security.authentication.AuthenticationManager找到一个bean,但找到了[studentAuth,teacherAuth]
可能对您有所帮助
set your group for page in secconfig:
.antMatchers("/page").hasAnyAuthority("USER,ADMIN").anyRequest()
and than you can work with groups in thymeleaf:
<div sec:authorize="hasAuthority('ADMIN')"></div>
在secconfig中为页面设置您的组:
.antMatchers(“/page”).hasAnyAuthority(“用户,管理员”).anyRequest()
而且,您还可以使用thymeleaf中的组:
或在控制器中:
@RequestMapping(value = {"/index"}, method =RequestMethod.GET)
public String index(Model model, Principal principal) {
Collection<?> auth =
SecurityContextHolder.getContext().getAuthentication().getAuthorities();
if(auth.contains(new SimpleGrantedAuthority("ADMIN"))
{
return "pages/indexForUser";
}
else if(auth.contains(new SimpleGrantedAuthority("USER")))
{
return "pages/indexForTeacher";
}
//ROLE_ANONYMOUS
}
@RequestMapping(value={”/index},method=RequestMethod.GET)
公共字符串索引(模型、主体){
集合身份验证=
SecurityContextHolder.getContext().getAuthentication().GetAuthories();
if(auth.contains(新的SimpleGrantedAuthority(“ADMIN”))
{
返回“pages/indexForUser”;
}
else if(auth.contains(新的SimpleGrantedAuthority(“用户”))
{
返回“pages/indexForTeacher”;
}
//匿名角色
}