我有一个使用专有 cookie 格式的网络应用程序。我还有一个 Spring Boot 服务,它需要与这个现有的应用程序进行互操作。
应用程序将发送一个 HTTP 请求并设置一个 EncryptedLoginToken
cookie 值。在 Spring Boot 应用程序中,很容易编写类似
boolean userHasPermission (String permission)
{
LoginToken token = LoginToken.decrypt(cookies['EncryptedLoginToken']);
return token.isValid () && token.hasPermission (permission);
}
Spring Boot 应用程序具有像
这样的控制器@PostMapping(value="example/request/path")
@PreAuthorize("hasAuthority('example_permission')")
public ResponseEntity<?> exampleRequest ()
{
// this will only be called if userHasPermission('example_permission') returns true
}
Spring Boot 应用目前实现了 HTTP 身份验证,必须同时支持该方法和该方法。
如何将 userHasPermission
插入 Spring boot 以进行 a) 用户身份验证和 b) 检查 hasAuthority
?
答案 0 :(得分:0)
您可以创建一个实现 PermissionEvaluator
的新组件。
@Component
public class DocumentsPermissionEvaluator
implements PermissionEvaluator {
@Override
public boolean hasPermission(Authentication authentication,
Object target,
Object permission) {
...
}
@Override
public boolean hasPermission(Authentication authentication,
Serializable targetId,
String targetType,
Object permission) {
...
}
}
如果您在 authentication
对象中没有适当的信息,您可以创建自定义过滤器和自定义身份验证,您可以在其中存储来自 cookie 的信息(如果需要)。
@PreAuthorize("hasPermission(#param, 'someType', 'SOME_PERMISSION')")
public Result doSomething(String param) {
return ...;
}