java - Spring-boot with spring secutiry 错误:出现意外错误(类型=禁止,状态=403)
问题描述
我目前正在通过 spring-boot 对我的小 API 实施安全性,作为学校的一个项目,当然没什么大不了的,但我想管理一些角色和东西。我一直在尝试添加 .antMatchers(url).hasRole(someRole)。...更多蚂蚁匹配器...
在测试登录时实际上显示错误(类型=禁止,状态=403)。
这是一些代码
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/control").hasRole("ADMIN")
.antMatchers("/", "/index","/img/*.jpg","/*.js","/*.css").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
public PasswordEncoder getPasswordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
只是一个控制器,它显示我的控制面板以 CRUD 我的动物:p
@Controller
@RequestMapping("/control")
public class AdminController {
@Autowired
private AnimalService animalService;
@RequestMapping
public String getAnimals(Model model) {
List<Animal> animals = animalService.getAnimals();
List<Animal> cats = new ArrayList<Animal>();
List<Animal> dogs = new ArrayList<Animal>();
List<Animal> bunnys = new ArrayList<Animal>();
List<Animal> rats = new ArrayList<Animal>();
animals.forEach(animal -> {
animal.setStr(Base64.getEncoder().encodeToString(animal.getImg()));
if (animal.getType().equals("cat")) {
cats.add(animal);
}
if (animal.getType().equals("dog")) {
dogs.add(animal);
}
if (animal.getType().equals("bunny")) {
bunnys.add(animal);
}
if (animal.getType().equals("rat")) {
rats.add(animal);
}
});
model.addAttribute("cats", cats);
model.addAttribute("dogs", dogs);
model.addAttribute("bunnys", bunnys);
model.addAttribute("rats", rats);
return "control";
}
}
我希望你们能帮助我,我是春天的新手。
解决方案
回复:就像在de DB中保存角色一样简单,如下所示:ROLE_roleNAME
推荐阅读
- python - numpy:在“锯齿状”数组中矢量化随机数生成
- c - 如果我使用了所有动态分配的内存,我是否必须释放它?
- vba - VBA 函数下拉菜单
- javascript - 如何将变量传递给函数
- java - 是否可以在不影响主键的情况下删除jdbc db表中的一行
- java - 我无法在 android studio 中将适配器附加到 recyclerview
- python - ipython 或 jupyter 中的制表符完成是为了建议替代方案而寻求什么?
- php - 如何使用php在数组中搜索
- python - 使用 python 将包括图像的数据写入 xlsx 或 xls 文件
- c# - C#:是否可以在不消耗/销毁元素的情况下枚举 BlockingCollection(不是请求枚举器时的快照)?