[英]Spring Security - permitAll() not allowing unauthenticated access
[英]Spring security not allowing access with simple matcher and permitAll
就Spring安全而言,這對我來說是全新的。 我在網上找到了許多資料,描述了如何設置基本安全性,並且能夠獲取HTTPS REST調用以在服務器端使用以下配置:
@Configuration
@EnableWebSecurity
@EnableConfigurationProperties(SecurityAuthProperties.class)
public class ServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
private final SecurityAuthProperties properties;
@Autowired
public ServerSecurityConfiguration(SecurityAuthProperties properties) {
this.properties = properties;
}
@Override
public void configure(HttpSecurity http) throws Exception {
properties.getEndpoints().forEach((key, value) -> {
try {
for (HttpMethod method : value.getMethods()) {
http.authorizeRequests().antMatchers(method, value.getPath()).permitAll().and()
.httpBasic().and().csrf().disable();
}
} catch (Exception e) {
throw new SecurityConfigurationException(
"Problem encountered while setting up endpoint restrictions", e);
}
});
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
但是,仔細檢查后,看起來好像有一部分(不確定多少)實際上已被禁用。 這可能就是為什么它允許來自客戶端的訪問嗎?
當我將配置修改為以下內容時,我總是收到響應“ Forbidden”。
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/rst/**").permitAll();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
在我看來,這段代碼將允許訪問/rst
及以下路徑中的任何內容,但事實恰恰相反。 我想念什么?
注意 :我還要提到的另一件事是,當前沒有“用戶”身份驗證。 “客戶端”不是基於Web的,而是具有自己的客戶端安全配置的單獨的Spring Boot服務。
更新 :
這是控制器之一:
@RestController
@RequestMapping("/rst/missionPlanning")
public class MissionPlannerController {
@Autowired
private MissionPlanner service;
@Autowired
private ThreadPoolTaskExecutor executor;
@PostMapping(value = "/planMission", produces = MediaType.APPLICATION_JSON_VALUE)
public DeferredResult<ResponseEntity<GeneralResponse>> planMission() {
DeferredResult<ResponseEntity<GeneralResponse>> result = new DeferredResult<>(60000L);
executor.execute(new Runner(result));
return result;
}
private class Runner implements ITask {
private DeferredResult<ResponseEntity<GeneralResponse>> result;
public Runner(DeferredResult<ResponseEntity<GeneralResponse>> result) {
this.result = result;
}
@Override
public void executeTask() {
// Invoke service and set result.
result.setResult(ResponseEntity.ok(service.planMission()));
}
}
}
更新 :
有趣。 我從另一個SO帖子( 帶有Spring-boot的安全配置 )中找到了一個似乎有效的示例。 唯一不同的是禁用CSRF。
我看到它代表“跨站點請求偽造”,但是我真的不明白這是什么,是否應該啟用它,如果可以,那么如何使它工作?
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().antMatchers("/rst/**").permitAll();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
設置控制器的方式可能有問題。 包含該路徑的控制器是否具有@RequestMapping("/rst")
?
如果您用控制器的外觀更新帖子,這將很有幫助。
編輯:看來您的問題是如果必須取消CSRF,則發出的請求類型。
CSRF要求在所有可能導致更改的請求方法上指定一個令牌(即POST,PUT,DELETE,PATCH,但不包括GET)。
這樣做的原因是,當您控制網頁時,它會增加一層安全性,僅允許您進行這些API調用。 如果沒有在請求中指定CSRF令牌,則惡意用戶將無法向您的服務提出該請求,因為CSRF令牌無法猜測。
您可以在此處了解更多信息: https : //docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html/csrf.html#csrf-include-csrf-token
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.