Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SpringMVC3.2和SpringSecurity3.1中使用方法安全性执行RunAs_Spring_Spring Mvc_Spring Security - Fatal编程技术网

如何在SpringMVC3.2和SpringSecurity3.1中使用方法安全性执行RunAs

如何在SpringMVC3.2和SpringSecurity3.1中使用方法安全性执行RunAs,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我有一个带有SpringMVC3.2和SpringSecurity3.1的web应用程序 我使用基于角色的安全性,并实现了UserDetailsService和UserDetails以提供授权 我已经使用jsr250注释启用了全局方法安全性 在登录用户方法访问限制为声明的角色的情况下,到目前为止的一切都正常工作 我还有一个进一步的要求,就是在应用程序初始化期间,作为一个具有“系统角色”的特殊用户运行某些调用的方法,理想情况下是按照JavaEE运行方式运行。 我不知道如何在春季安全中做到这一点 我

我有一个带有SpringMVC3.2和SpringSecurity3.1的web应用程序

我使用基于角色的安全性,并实现了UserDetailsService和UserDetails以提供授权

我已经使用jsr250注释启用了全局方法安全性

在登录用户方法访问限制为声明的角色的情况下,到目前为止的一切都正常工作

我还有一个进一步的要求,就是在应用程序初始化期间,作为一个具有“系统角色”的特殊用户运行某些调用的方法,理想情况下是按照JavaEE运行方式运行。 我不知道如何在春季安全中做到这一点

我是否应该尝试使用一些虚构的值和“系统角色”权限创建一个
预验证身份验证令牌

然后我可以做一些类似于
SecurityContextHolder.getContext().setAuthentication(令牌)的事情
初始化应用程序时

或者,我应该尝试使用RunAsManager。这听起来像是我需要的,但我还没有找到任何简单的例子来说明我如何实际使用它


我是Spring Security的新手,我不确定最好的方法

您真的需要为上述应用程序初始化附加角色吗?为什么不提取初始化期间需要运行的代码,如下所示:

public interface Service {

    @Secured("hasRole('USER')")
    void service();
}

public class DefaultService implements Service {

    @Override
    public void service() {
        doService();
    }

    public void doService() {
        // Implementation here
    }
}

...

public class AppInitializer {

    @Autowired
    private DefaultService service;

    public void init() {
        service.doService();
    }
}

我相信在这种情况下,一个很好的解决方案是使用SpringSecurityOAuth,因为它允许您与自定义规则进行更大的集成,以便通过令牌进行访问


当我的应用程序启动时

  • 我在Springbean中运行一个post-construct方法,在内存中创建一个具有系统角色的特殊用户
  • 此用户对象实现 org.springframework.security.core.userdetails.UserDetails


起初,这种方法看起来不错,但如果DefaultService要求自动连接其他bean,那么我需要使用基于类的注入来自动连接它们,因为它们的接口将有自己的安全注释。这会导致代理和事务支持出现问题,请参阅 org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken
@Service
@Transactional(readOnly = true)
public class ApplicationConfiguration{
    @Inject
    MyService myService;
    @PostConstruct
    @Transactional(readOnly = false)
    public void init(){

        // ######## Application Starting #######"

        // Create a user that meets the contract of the Spring UserDetails interface

        UserAccountImpl sysAcc = new UserAccountImpl("system", "system", "system");
        UserRole role = new UserRole(Role.SYSTEM_ROLE);
        role.addUserPermission(Permission.SYSTEM);
        sysAcc.addUserRole(role);
        UserDetailsAdapter userDetails = new UserDetailsAdapter(sysAcc);

        // Create a token and set the security context

        PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(token);

        // Now call service method with roles allowed  

        myService.initialiseSystem();
    }
}
public interface MyService {
    @RolesAllowed(SYSTEM)
    public void initialiseSystem();
}