【发布时间】:2018-03-14 18:30:00
【问题描述】:
我正在解决这个问题...我有一个带有 S2S 通信的 Spring Boot 应用程序。我有一个@RestController 方法,它应该接受 POST 请求。
这是控制器
@RestController
public class PaymentRestController {
@PostMapping("/util/paymentResponse")
public void savePaymentResponse(@RequestParam boolean transaction_status, @RequestParam String usedToken,
@RequestParam String transaction_message, @RequestParam String authCode,
@RequestParam String transactionCode, @RequestParam String orderId, HttpServletRequest request) {
//business logic
}
}
如果我点击此链接,我会收到 405 错误,方法不允许
第一次我发现请求被 web 应用程序上启用的 CSFR 过滤器阻止了,所以我以这种方式配置了我的安全性
@Configuration
@ComponentScan("it.besmart")
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
@Qualifier("customUserDetailsService")
UserDetailsService userDetailsService;
@Autowired
CustomSuccessHandler customSuccessHandler;
@Autowired
CustomAuthenticationFailureHandler customAuthenticationFailureHandler;
@Autowired
DataSource dataSource;
private final static Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class);
@Autowired
public void configureGlobalService(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SwitchUserFilter switchUserFilter() {
SwitchUserFilter filter = new SwitchUserFilter();
filter.setUserDetailsService(userDetailsService);
filter.setSuccessHandler(customSuccessHandler);
filter.setFailureHandler(customAuthenticationFailureHandler);
return filter;
}
protected void configure(HttpSecurity http) throws Exception {
logger.debug("Webapp security configured");
http
.authorizeRequests()
.antMatchers("/", "/home", "/contacts", "/faq", "/privacy", "/register", "/registrationConfirm", "/util/**", "/resendRegistrationToken","/park**", "/oauth/authorize", "/error")
.permitAll()
.antMatchers("/profile**", "/edit**","/payment**", "/plate**","/notification**", "/addPaymentMethod**", "/logout/impersonate**")
.access("hasRole('USER') or hasRole('NOPAYMENT')")
.antMatchers("/book**", "/manage**")
.access("hasRole('USER')")
.antMatchers("/admin**", "/login/impersonate**").access("hasRole('ADMIN')")
.antMatchers("/updatePassword").hasAuthority("CHANGE_PASSWORD_PRIVILEGE")
.and().formLogin().loginPage("/?login=login").loginProcessingUrl("/") .successHandler(customSuccessHandler).failureHandler(customAuthenticationFailureHandler).usernameParameter("email").passwordParameter("password").and().rememberMe().rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository()).tokenValiditySeconds(86400).and().exceptionHandling().accessDeniedPage("/accessDenied")
.and().csrf().ignoringAntMatchers( "/util**")
.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/?logout=true").permitAll()
.and().addFilterAfter(switchUserFilter(), FilterSecurityInterceptor.class);
}
通过这种方式,我没有收到 CSRF 令牌异常,但仍然收到 405 错误。 这甚至不是 POST 的问题,因为如果我更改为 GET 请求和映射,我仍然会出现 405 错误......如果我尝试发送 POST,我在标题响应中看到 Allowed 方法是 POST,如果我在 GET 中发送它,我会看到允许的方法 POST... 很奇怪
我不知道在哪里看...
【问题讨论】:
-
只是为了排除与安全相关的问题,您可以尝试将属性 management.security.enabled=false 添加到您的 application.properties 并说明它是否可以这样工作?
-
什么都没有改变...问题仍然存在...
-
所以看起来不是安全问题...您如何发布您的请求?此外,尝试将 required=false 添加到所有 RequestParam,调试应用程序并在控制器的第一行放置断点。
-
像这样:@RequestParam(value = "yourParamName", required = false)
-
你是对的!有一个空参数!但奇怪的是我收到 405 错误...
标签: java spring rest spring-security