首页 新闻 赞助 找找看

Springboot简单整合Security基于数据库默认jdbcAuthentication方法,登录上来了,全部权限无法访问

0
悬赏园豆:5 [已解决问题] 解决于 2022-04-08 17:18

数据库很简单,一个记录

SecurityConfig继承WebSecurityConfigurerAdapter重写了认证和授权的方法

完整代码:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页进入系统所有人可以访问,功能页只有对应有权限的人才能访问
        //请求授权的规则~
        //链式编程
        //授权
        /**
         * HTTP请求处理
         */
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/user/vip").hasAnyRole("vip","svip","ssvip")              //给角色授权
                .antMatchers("/user/svip").hasAnyRole("svip","ssvip")
                .antMatchers("/user/**").hasRole("ssvip")
            .and()
            .csrf().disable();

        //没有权限默认到登录页面。需要开启登录页面
        http.formLogin();
    }

    @Autowired
    DataSource dataSource;
    /**
     * 授权验证服务
     */
    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        //内存中
        /*auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
                .withUser("simm").password("123").roles("USER").and()
                .withUser("admin").password("admin").roles("USER","ADMIN");*/
       /* auth
                .inMemoryAuthentication()
                .passwordEncoder(new BCryptPasswordEncoder())//在此处应用自定义PasswordEncoder
                .withUser("root")
                .password(new BCryptPasswordEncoder().encode("root"))
                .roles("ssvip")
                .and()
                .withUser("user")
                .password(new BCryptPasswordEncoder().encode("user"))
                .roles("vip");
*/
        //默认
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("select user_name,user_password, 'true' as enabled from users WHERE user_name=?")  //认证
                .authoritiesByUsernameQuery("select user_name,user_role from users where user_name=?")     //授权
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
     public  BCryptPasswordEncoder BCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
     }
}

我先做了一次内存认证,没有问题,认证和权限都可以成功,这里的权限是优先级,ssvip>svip>vip

前端页面


点击去就是相应的内容页,这样的,三个页面,body都是一句话:

但是使用了基于数据库认证,可以登录上,但是点哪个都没有权限:

希望大神求解

_学习的主页 _学习 | 菜鸟二级 | 园豆:246
提问于:2022-04-08 11:10
< >
分享
最佳答案
0

user_role的数据改一下,WebExpressionVoter.vote打断点就能看到了

_学习 | 菜鸟二级 |园豆:246 | 2022-04-08 17:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册