gpt4 book ai didi

java - 仅针对内容类型 x-www-form-urlencoded 接受 Spring + Spring Security 请求

转载 作者:行者123 更新时间:2023-11-30 06:07:32 25 4
gpt4 key购买 nike

这是我第一次决定使用完整的 Java 代码配置来编写基于 Spring Boot 和 Spring Security 的应用程序,但我遇到了无法超越的奇怪问题。我正在尝试使用 Postman 测试 API,并且仅当我使用内容类型为 application/x-www-form-urlencoded 时,我的请求才会被接受。下面我粘贴了我当前的所有配置。

@SpringBootApplication
public class OpenIdApplication {
public static void main(String[] args) {
SpringApplication.run(OpenIdApplication.class, args);
}
}

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private UserService userService;

@Autowired
public SecurityConfig(UserService userService) {
this.userService = userService;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(11);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.cors()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}

}

@RestController
public class UserController {
...

@PostMapping(value = "/register")
public ResponseEntity<Object> registerUser(
@RequestBody UserRegistrationDto newUser, BindingResult bindingResult) {
...
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserRegistrationDto {
private String username;
private String firstName;
private String lastName;
private String email;
private String password;
private String passwordRepeat;
}

如果我想登录,我只能使用提到的内容类型来执行此操作。对于 application/json 我收到 403 响应状态。对于/registration端点,如果我从方法参数中删除@RequestBody,那么它可以很好地处理application/x-www-form-urlencoded请求,但如果我保留它这个内容类型我得到 415,但如果我尝试使用 application/json 那么我会看到 403。我还尝试在此端点上方添加 @PreAutorize("permitAll()") 注释,添加httpBasic() 到 Spring Security 配置中 - 这导致响应状态从 403 更改为 401。

我尝试向该端点发送 2 个不同的 JSON:

{
"username":"test",
"firstName":"Test",
"lastName":"Test",
"email":"test@test.com",
"password":"test",
"passwordRepeat":"test",
"_csrf":"8b7d4680-5be4-482a-9138-b4eb92a358c1"
}

{
"newUser": {
"username":"test",
"firstName":"Test",
"lastName":"Test",
"email":"test@test.com",
"password":"test",
"passwordRepeat":"test"
},
"_csrf":"8b7d4680-5be4-482a-9138-b4eb92a358c1"
}

当然,每次我都会确保 _csrf 与我的 API 返回的匹配。

我正在使用 Spring Boot 2.0.1.RELEASE 和 Spring Security 5.0.3.RELEASE。

最佳答案

我相当确定 Ant 模式 "/register/**" 匹配所有以 /register/ ( See examples here ) 开头的 URL,而您发布到 /register (末尾没有斜线)。你应该尝试一下

.antMatchers("/register*").permitAll()

.mvcMatchers(HttpMethod.POST, "/register").permitAll()

第一个匹配任何以 /register 开头的 URL,而第二个则完全匹配您的 @PostMapping

关于java - 仅针对内容类型 x-www-form-urlencoded 接受 Spring + Spring Security 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50993166/

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