Spring Security-Web项目设置权限

本文详细介绍了如何在SpringSecurity中通过配置文件、配置类和自定义UserDetailsService实现Web项目的登录权限设置,包括使用BCryptPasswordEncoder加密密码的过程。
摘要由CSDN通过智能技术生成

Web 项目设置权限

设置登录的用户名和密码

第一种方式:通过配置文件

第二种方式:通过配置类

第三种方式:自定义编写实现类UserDetailsService接口

通过配置文件

在application.yaml 中 增加 用户名为 ly 密码为 abc

 spring:
   security:
     user:
       name: ly
       password: abc

重启, 发现 控制台没有默认密码了, 按照配置 输入用户名密码即可 显示

通过配置类

注释调 之前在 application.yaml 中配置的 security的用户名及密码

(如果 出现 There is no PasswordEncoder mapped for the id "null" 记得 注入 PasswordEncoder )

 
package com.config;
 ​
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 ​
 @Configuration    //配置类
 public class SecurityConfig extends WebSecurityConfigurerAdapter {
     
     @Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
         String password = bCryptPasswordEncoder.encode("123");
         auth.inMemoryAuthentication().withUser("abc").password(password).roles("admin");
     }
     //注入bean 否则报错 There is no PasswordEncoder mapped for the id "null"
     @Bean
     PasswordEncoder passwordEncoder(){
         return new BCryptPasswordEncoder();  
     }
 }

启动 运行, 用户名 abc 密码 123 即可看到页面

自定义编写实现类 (查询数据库)

第一步 创建配置类 设置使用哪个UserDetailsService实现类

第二步 编写 UserDetailsService实现类 返回User对象 有用户名及密码 及权限

创建配置类

 package com.config;
 ​
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 ​
 @Configuration    //配置类
 public class SecurityConfig extends WebSecurityConfigurerAdapter {
 ​
     @Autowired
     UserDetailsService userDetailsService;
 ​
 ​
     @Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
     }
     @Bean
     PasswordEncoder passwordEncoder(){
         return new BCryptPasswordEncoder();
     }
 }

创建接口实现类

 package com.service;
 ​
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.AuthorityUtils;
 import org.springframework.security.core.userdetails.User;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
 ​
 import java.util.List;
 ​
 @Service("userDetailsService")
 public class MyUserDetailsService implements UserDetailsService {
     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 ​
         List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
         return new User("lisi",new BCryptPasswordEncoder().encode("lisi"),list);
     }
 }

启动项目 用户名 lisi 密码 lisi 即可看到页面

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

射手座的程序媛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值