【发布时间】:2019-11-04 21:43:08
【问题描述】:
我有一个基于 Spring Security + MVC 的项目。
该项目的 Web 方面已经由以前的开发人员开发。我的任务是公开一些 API 服务提供商将对其进行回调。
为了存档,我完成了以下代码。
@RestController
public class LeegalityController {
private static final Logger logger = LoggerFactory.getLogger(LeegalityController.class);
@RequestMapping(value = "webhook/{user}/{id}", method = RequestMethod.POST)
public ResponseEntity<String> webhook(@PathVariable("user") int user, @PathVariable("id") String id,
@RequestBody LeegalityReqResp reqResp, HttpServletRequest request) {
try {
logger.info("webhook()== user[" + user + "] == id[" + id + "]");
logger.info("webhook()== LeegalityReqResp-->" + reqResp.toString());
} catch (Exception e) {
logger.error("webhook() Error==[" + e.getMessage() + "]", e);
}
return new ResponseEntity<String>("success", HttpStatus.OK);
}
}
但是在通过邮递员测试 API 时出现以下错误
我已尝试对安全配置 xml 进行一些更改,如下所示
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<http pattern="*/webhook/**" security="none" create-session="stateless" />
<http auto-config="true">
<intercept-url pattern="/"
access="hasAnyRole('ROLE_ADMIN','ROLE_ONE','ROLE_TWO')" />
<!-- <intercept-url pattern="/webhook/**" method="POST" access="permitAll" /> -->
<intercept-url pattern="/admin"
access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/otp"
access="hasAnyRole('ROLE_ONE','ROLE_TWO')" />
<form-login login-page="/ProjectName-Home"
default-target-url="/" authentication-failure-url="/login?error"
username-parameter="username" password-parameter="password" />
<logout logout-success-url="/login?logout" />
</http>
<authentication-manager
alias="authenticationManager">
<authentication-provider
ref="userAuthenticationProvider">
</authentication-provider>
</authentication-manager>
<beans:bean id="userAuthenticationProvider"
class="com.java.ProjectName.service.user.UserAuthenticationProvider"></beans:bean>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength"
value="11" />
</beans:bean>
</beans:beans>
我应该怎么做才能确保这个 /webhook/* 被排除在 CSRF 验证之外。感谢您提供任何帮助,并提前感谢您。
我的 spring 版本是 4.2.0.RELEASE,spring security 版本是 4.0.2.RELEASE。
【问题讨论】:
-
如果你想排除
/webhook/*,为什么在你的配置中使用*/webhook/**? -
我已经尝试过 /** 和 /*
-
你试过
/webhook/*吗?/**和/*没有任何意义,因为您不会对所有 URL 都有任何安全性。 -
'/ProjectName/webhook/' 其中 projectName 是 URL 的主要部分
标签: java spring spring-security csrf