我想允许所有用户访问标签页面,但应用程序每次都会保留登录页面。这是我的配置文件
@Configuration
@EnableWebSecurity
public class SpringBootSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("root").password("{noop}password").roles("admin");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/tags").permitAll()
.antMatchers("/admin/**").hasAnyRole("admin")
.and()
.formLogin().loginPage("/login").permitAll();
}
}
控制器的一些方法
@GetMapping("/tags")
public String showAllTags(Model model) {
model.addAttribute("tags", tagRepository.findAll());
return "all_tags";
}
@GetMapping("/admin/edit/tag/{id}")
public String showUpdateForm(@PathVariable("id") long id, Model model) {
Tag tag = tagRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("tag", tag);
return "update_tag";
}
@PostMapping("/admin/update/tag/{id}")
public String updateTag(@PathVariable("id") long id, Tag tag,
BindingResult result, Model model) {
if (result.hasErrors()) {
tag.setId(id);
return "update_tag";
}
tagRepository.save(tag);
return "redirect:/tags";
}
事实上,看起来我的残疾保安根本不起作用。当我访问/标签时,它会显示登录页面。
删除pom.xml文件中的以下安全依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>