ned*_*nom 4 java spring-security spring-boot
我正在设置一个 Spring Boot 1.3 安全应用程序,但具有公众无法访问的管理端口,因此我不需要该端口上的任何安全性。
这就是我想要实现的目标:
server.port = 8080 # -> secure
management.port = 8081 # -> unsecure
Run Code Online (Sandbox Code Playgroud)
但是,一旦我添加 WebSecurityConfigurerAdapter,它就会自动对两个端口生效。如果管理端口不同,设置management.security.enabled=false没有效果,这是一个bug吗?我怎样才能仅禁用管理端口的安全性?
我的简单安全配置:
@Configuration
@EnableWebSecurity
static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道一个可能的解决方法是设置自定义上下文路径,例如。/manage并从安全性中忽略此路径,但使用非标准路径加上摆弄将路径解析为安全配置而不对其进行硬编码似乎并不理想,所以我想知道是否有标准方法对此。
您始终可以添加请求匹配器并跳过管理端口的安全检查。Spring Boot 2 的解决方法如下。这可能也适用于较旧的 Spring Boot 版本。请尝试看看。
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
// this is the port for management endpoints
@Value("${management.server.port}")
private int managementPort;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers(checkPort(managementPort)).permitAll()
.anyRequest().authenticated();
}
/**
* This method verifies whether a request port is equal to the provided method parameter
*
* @param port Port that needs to be checked with the incoming request port
* @return Returns a request matcher object with port comparison
*/
private RequestMatcher checkPort(final int port) {
return (HttpServletRequest request) -> port == request.getLocalPort();
}
}
Run Code Online (Sandbox Code Playgroud)
受到这个答案的启发。
| 归档时间: |
|
| 查看次数: |
3911 次 |
| 最近记录: |