gpt4 book ai didi

spring - 使用 spring-security 更改方法调用的安全上下文

转载 作者:行者123 更新时间:2023-12-04 04:05:44 24 4
gpt4 key购买 nike

目前我正在使用 spring security 和 @PreAuthorize用于保护方法调用的注释。现在我想更改方法调用的身份验证 token ,例如 run-as authentication replacement Spring 安全允许我做。

我可以在每个方法的基础上配置替换吗?每个注释,SpEL 表达式....
如果没有,是否有可能在 runAsManager 中找出调用了什么方法?
我将如何配置安全对象的安全配置属性?

最佳答案

我已发帖 a detailed article关于与 @PreAuthorize 一起实现 Run-As .

1) 实现你自己的 RunAsManager创建 Authentication在基于任何自定义逻辑的方法执行期间使用。下面的示例使用提供额外角色的自定义注释:

public class AnnotationDrivenRunAsManager extends RunAsManagerImpl {

@Override
public Authentication buildRunAs(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
if(!(object instanceof ReflectiveMethodInvocation) || ((ReflectiveMethodInvocation)object).getMethod().getAnnotation(RunAsRole.class) == null) {
return super.buildRunAs(authentication, object, attributes);
}

String roleName = ((ReflectiveMethodInvocation)object).getMethod().getAnnotation(RunAsRole.class).value();

if (roleName == null || roleName.isEmpty()) {
return null;
}

GrantedAuthority runAsAuthority = new SimpleGrantedAuthority(roleName);
List<GrantedAuthority> newAuthorities = new ArrayList<GrantedAuthority>();
// Add existing authorities
newAuthorities.addAll(authentication.getAuthorities());
// Add the new run-as authority
newAuthorities.add(runAsAuthority);

return new RunAsUserToken(getKey(), authentication.getPrincipal(), authentication.getCredentials(),
newAuthorities, authentication.getClass());
}
}

此实现将查找自定义 @RunAsRole protected 方法上的注释(例如 @RunAsRole("ROLE_AUDITOR") ),如果找到,会将给定的权限(在本例中为 ROLE_AUDITOR)添加到授予权限列表中。 RunAsRole本身只是一个简单的自定义注解。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RunAsRole {
String value();
}

2)实例化管理器:
<bean id="runAsManager"
class="org.springframework.security.access.intercept.RunAsManagerImpl">
<property name="key" value="my_run_as_key"/>
</bean>

3)注册:
<global-method-security pre-post-annotations="enabled" run-as-manager-ref="runAsManager">
<expression-handler ref="expressionHandler"/>
</global-method-security>

4) Controller 中的示例用法:
@Controller
public class TransactionLogController {

@PreAuthorize("hasRole('ROLE_REGISTERED_USER')") //Authority needed to access the method
@RunAsRole("ROLE_AUDITOR") //Authority added by RunAsManager
@RequestMapping(value = "/transactions", method = RequestMethod.GET) //Spring MVC configuration. Not related to security
@ResponseBody //Spring MVC configuration. Not related to security
public List<Transaction> getTransactionLog(...) {
... //Invoke something in the backend requiring ROLE_AUDITOR
{

... //User does not have ROLE_AUDITOR here
}

编辑: key的值在 RunAsManagerImpl可以是你想要的任何东西。这是 Spring docs 的摘录关于它的使用:

To ensure malicious code does not create a RunAsUserToken and present it for guaranteed acceptance by the RunAsImplAuthenticationProvider, the hash of a key is stored in all generated tokens. The RunAsManagerImpl and RunAsImplAuthenticationProvider is created in the bean context with the same key:

<bean id="runAsManager"
class="org.springframework.security.access.intercept.RunAsManagerImpl">

<bean id="runAsAuthenticationProvider"
class="org.springframework.security.access.intercept.RunAsImplAuthenticationProvider">

By using the same key, each RunAsUserToken can be validated it was created by an approved RunAsManagerImpl. The RunAsUserToken is immutable after creation for security reasons.

关于spring - 使用 spring-security 更改方法调用的安全上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11667086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com