学习日记 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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大