学习日记 2022-04-08 13:23 采纳率: 100%
浏览 66
已结题

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

数据库很简单,一个记录

img

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

前端页面

img

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

img

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

img

==虚心求解==

  • 写回答

1条回答 默认 最新

  • a1767028198 2022-04-08 14:35
    关注

    看了下源码,基本你这配置没啥问题,org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl#loadUserByUsername方法中dbAuths就是你的角色信息了

    img


    把代码搞个压缩包丢网盘看看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月16日
  • 已采纳回答 4月8日
  • 创建了问题 4月8日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器