如何使用spring安全配置从请求头验证用户角色

如何使用spring安全配置从请求头验证用户角色,spring,spring-security,taglib,servlet-3.0,Spring,Spring Security,Taglib,Servlet 3.0,我已经使用taglibs添加了spring安全性,用于在应用程序中加载基于角色的内容。认证过程将由外部服务负责,认证成功后,外部服务将在请求头中添加用户详细信息和角色详细信息。在我的应用程序中,我必须从请求头获取角色,并需要使用spring安全配置方法验证角色 请帮助我验证角色 春季安全等级: @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdap

我已经使用taglibs添加了spring安全性,用于在应用程序中加载基于角色的内容。认证过程将由外部服务负责,认证成功后,外部服务将在请求头中添加用户详细信息和角色详细信息。在我的应用程序中,我必须从请求头获取角色,并需要使用spring安全配置方法验证角色

请帮助我验证角色

春季安全等级:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(final HttpSecurity http) throws Exception {

        http.authorizeRequests()
            .antMatchers("/login").permitAll()
            .antMatchers("/console")
            .access("hasRole('MASTER ADMIN') or hasRole('ADMIN') or hasRole('DEV') or hasRole('QA')")
            .and()
            .formLogin()
            .defaultSuccessUrl("/console", true)
            .and().logout().logoutSuccessUrl("/login");
    }
}
控制器类:

    @RequestMapping(value = "/login")
    public ModelAndView validateLogin(final HttpServletRequest request, final HttpServletResponse response) {
        final ModelAndView modelView = new ModelAndView();
        final String loginUserId = request.getParameter("USER");
        final String companyCode = request.getHeader("ROLE");
        final String firstName = request.getHeader("FIRSTNAME");
        final String lastName = request.getHeader("LASTNAME");

//** some code to validate the role details with spring security configure method ie (has access method) and return the default success url based on the role.

嗯,我想这可能对你有帮助。UserDetails服务负责为主体定义角色。创建这些角色的方式可以是访问数据库,也可以是从请求头获取角色。我不是在讨论从请求头获取角色是好是坏,我所做的是为您的问题提供一个可能的解决方案:D。我还假设您想使用@PreAuthorize(“hasRole('MASTER ADMIN'))”)@AdrianClaudiuDima谢谢。UserDetailServiceImpl不适合我的需要。因为我不会从DB或外部服务获取任何细节。所有用户信息将在请求头中可用。我只需要使用spring security configure验证请求头中的角色method@AdrianClaudiuDimaSpring security Overrided configure方法定义了主管理员、管理员等的访问权限。我必须验证请求头(已由siteminder验证)该请求是否具有上述任何角色。