我正在使用最新的 Spring Boot + Spring Boot Starter Security 来构建简单的代理应用程序。目标是使用单一路由/方法启动应用程序:
@RequestMapping(value = "/api/register",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<?> register(Registration registration) {
安全配置为:
@Override
protected void configure(HttpSecurity http) throws Exception {
this.http = http
.authorizeRequests()
.antMatchers("/api/register").hasAuthority(AuthorityConstants.ADMIN)
.and();
}
public HttpSecurity getHttpSecurity() {
return http;
}
该应用程序的目标是接受以下形式的注册请求:
{
"route":"/api/foo/bar",
"proxy_location": "http://back-end-server/path/to/resource",
"role": "some-authority"
}
然后应用程序会添加一个/api/foo/bar
使用预定义方法进行路由,该方法将代理(转发)未来的请求到后端服务。
我知道这有点愚蠢,真正的用例涉及 websockets 和动态创建主题。
我面临的问题是,SecurityConfigurer 完成后我似乎无法更新安全配置。
在上面的代码示例中,我正在缓存HttpSecurity
给我的对象SecurityConfigurer
然后尝试再次使用该对象来配置新路由:
@Inject
private SecurityConfigurer security;
@RequestMapping(value = "/api/register",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<?> allowGetAccounts(Registration registration) {
try {
security.getHttpSecurity()
.authorizeRequests()
.antMatchers(registration.getRoute()).hasAuthority(registration.getRole());
...
} catch (Exception e) {
log.error("Updating security failed!", e);
}
return new ResponseEntity<>(null, HttpStatus.OK);
}
有没有办法在运行时动态更新安全配置?
另外,如果有人有任何关于动态创建 websocket 主题的注释,我们也将不胜感激!