gpt4 book ai didi

java - Spring启动与Spring安全错误: There was an unexpected error (type=Forbidden,状态= 403)

转载 作者:行者123 更新时间:2023-12-01 17:18:55 24 4
gpt4 key购买 nike

我目前正在使用 spring-boot 为我的小 API 实现安全性,作为学校的一个项目,当然没什么大不了的,但我想管理一些角色和东西。我一直在尝试添加 .antMatchers(url).hasRole(someRole)。 ...更多 Ant 匹配器...
测试登录时实际显示错误(type=Forbidden,status=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();
}

只是一个 Controller ,显示我的控制面板以 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";
}

}

我希望你们能帮助我,我是 Spring 的新手。

最佳答案

DB tuple

回复:就像将角色保存在 de DB 中一样简单,如下所示:ROLE_roleNAME

关于java - Spring启动与Spring安全错误: There was an unexpected error (type=Forbidden,状态= 403),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61336441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com