【发布时间】:2019-09-22 01:59:24
【问题描述】:
我正在运行 Spring Boot 应用程序,但无法从 HTML 表单中请求参数。每当我尝试使用 POST 方法通过表单发送参数时,它都会引发 Forbidden 403 错误。即使我将 spring 安全设置更改为 anyRequest.permitAll,它仍然会抛出 403。知道如何解决这个问题吗?
mastermind_home.jsp:
<form action="/mastermind/home" method="POST">
<label>
<input type="radio" name="difficulty" value="easy">
</label>Easy<br>
<label>
<input type="radio" name="difficulty" value="medium" checked>
</label>Medium<br>
<label>
<input type="radio" name="difficulty" value="hard">
</label>Hard<br>
<input type="submit" value="Start game">
</form>`
控制器:
@AllArgsConstructor
@Controller
@RequestMapping("mastermind")
public class MastermindController {
private MastermindServiceImpl mastermindService;
private UserService userService;
@GetMapping("/home")
public String prepareMmHomePage() {
return "mastermind_home";
}
@PostMapping("/home")
public String formRequest( @RequestParam String difficulty) {
return "mastermind_home";
}
Spring 安全设置:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private DataSource dataSource;
public SecurityConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
public Authentication authentication() {
return SecurityContextHolder.getContext().getAuthentication();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery("SELECT user_name, password, true FROM users WHERE user_name = ?")
.authoritiesByUsernameQuery("SELECT user_name, 'ROLE_USER' FROM users WHERE user_name = ?");
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/main_menu","/mastermind/**").authenticated()
.antMatchers("/admin", "/admin/**").hasRole("ADMIN")
.antMatchers("/home", "/register", "/registered_successfully").anonymous()
.anyRequest().authenticated()
.and()
.formLogin()
.defaultSuccessUrl("/main_menu")
.and()
.logout()
.logoutSuccessUrl("/home");
}
}
【问题讨论】:
标签: spring-boot jsp spring-security