Spring boot spring引导spring security基于自定义令牌的身份验证和自定义授权

Spring boot spring引导spring security基于自定义令牌的身份验证和自定义授权,spring-boot,spring-security,Spring Boot,Spring Security,大家好,我正在开发应用程序,在我的应用程序中使用SpringBoot和SpringSecurity进行身份验证,我使用自定义令牌,我能够成功地对用户进行身份验证。 现在,我想向我的应用程序添加自定义授权,我想通过以下方式获得授权: 应用程序的用户存储在数据库中,角色以及与角色相关的相应权限将存储在数据库中。我在网上浏览了很多文章,但在所有文章中,用户的角色通常都是用预授权方法硬编码的,比如预授权(hasRole(Role_admin))或预授权(hasRole(Role_user))您能帮我提供

大家好,我正在开发应用程序,在我的应用程序中使用SpringBoot和SpringSecurity进行身份验证,我使用自定义令牌,我能够成功地对用户进行身份验证。 现在,我想向我的应用程序添加自定义授权,我想通过以下方式获得授权:

应用程序的用户存储在数据库中,角色以及与角色相关的相应权限将存储在数据库中。我在网上浏览了很多文章,但在所有文章中,用户的角色通常都是用预授权方法硬编码的,比如预授权(hasRole(Role_admin))或预授权(hasRole(Role_user))您能帮我提供一些解决方案,以便将角色的价值与保存在关系数据库中的价值进行比较吗?使用定制的UserDetails服务,我可以从数据库中获取用户对象,但不能获取此授权内容。如果您有此方面的链接,请告诉我

我当前的安全配置如下:

@EnableWebMvcSecurity
@EnableWebSecurity(debug = false)
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private DashBoardUserService dashBoadUserService;

    @Autowired 
    private TokenUtils tokenUtils;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.authorizeRequests().antMatchers(IConstants.CAMEL_URL_MAPPING).hasRole(DashBoardUserService.ROLE_USER);
        http.headers().frameOptions().disable();
        SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityConfigurerAdapter = new XAuthTokenConfigurer(
                userDetailsServiceBean(),tokenUtils);
        http.apply(securityConfigurerAdapter);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        authManagerBuilder.userDetailsService(dashBoadUserService);
    }


    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
您可以使用WebSecurityConfigureAdapter,在这里您可以使用datasource通过sql获得身份验证

完整示例

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    DataSource dataSource;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select username,password,enabled from s_admin where username=?")
                .authoritiesByUsernameQuery("select username,role from s_admin_roles where username=?");
    }
}

您好,wcong,看到编辑过的问题,谢谢您的回复。您能给我建议任何答案吗?我还需要输入有关如何从数据库中获取securityconfigs http.authorizeRequests().antmatchers().hasRole(“Role_Admin”)Role_Admin字段中的值,以及如何使用预授权注释检查方法级别的安全性
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    DataSource dataSource;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select username,password,enabled from s_admin where username=?")
                .authoritiesByUsernameQuery("select username,role from s_admin_roles where username=?");
    }
}