网站首页 > 技术文章 正文
自定义登录的用户名密码
第一种方式:通过配置文件application.properties设置
spring.security.user.name=superadmin
spring.security.user.password=superadmin
第二种方式:通过配置类
新建配置类SecurityConfig继承WebSecurityConfigurerAdapter重写configure(AuthenticationManagerBuilder auth)方法在里面进行账户密码角色的设置
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String password = bCryptPasswordEncoder.encode("123456");
auth.inMemoryAuthentication().withUser("superadmin").password(password).roles("admin");
}
@Bean
PasswordEncoder password(){
return new BCryptPasswordEncoder();
}
}
另外要提的一点是WebSecurityConfigurerAdapter类在5.7版本被@Deprecated所标记了
第三种方式:自定义编写实现类,实现UserDetailsService接口,将用户认证提取到业务层进一步实现个性化
第一步 创建配置类,设置使用哪个userDetailsService实现类
@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(password());
}
@Bean
PasswordEncoder password(){
return new BCryptPasswordEncoder();
}
}
第二步 编写实现类,返回User对象,User对象有用户名密码和操作权限
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
return new User("admin",new BCryptPasswordEncoder().encode("123456"),auths);
}
}
运行登录验证没问题
查询数据库完成认证
进一步抽离,将用户信息存储到数据库中,更符合实际应用,例子使用了mybatis-plus
第一步 准备库表,基础类
CREATE TABLE users (
id int NOT NULL,
username varchar(45) DEFAULT NULL,
password varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into security_demo.users values(1,'admin','123456');
@Data
@TableName("users")
public class UsersEntity {
private Integer id;
private String username;
private String password;
}
@Repository
public interface UsersMapper extends BaseMapper<UsersEntity> {
}
第二步 实现登录验证
在loadUserByUsername方法中编写登录验证过程
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UsersMapper usersMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
QueryWrapper<UsersEntity> wrapper = new QueryWrapper<>();
wrapper.eq("username",username);
wrapper.last(" limit 1");
UsersEntity user = usersMapper.selectOne(wrapper);
if(user ==null || user.getId() == null){
throw new UsernameNotFoundException("用户名或密码错误");
}
List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
return new User("admin",new BCryptPasswordEncoder().encode("123456"),auths);
}
}
验证,使用数据库中的数据可以登录
自定义登录页面
在配置类SecurityConfigTest重写protected void configure(HttpSecurity http)方法实现相关配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
//登录页面设置
.loginPage("/login.html")
//登录访问路径
.loginProcessingUrl("/user/login")
//登录成功后跳转的页面
.defaultSuccessUrl("/test/index").permitAll()
//设置白名单
.and().authorizeRequests()
.antMatchers("/test/hello","/user/login").permitAll()
//关闭ssrf防护
.anyRequest().authenticated()
.and().csrf().disable();
}
编写配置类中相关页面与接口
在resources.static文件夹中创建login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/login" method="post">
用户名:<input type="text" name="username">
<br/>
密 码:<input type="text" name="password">
<br/>
<input type="submit" value="login">
</form>
</body>
</html>
TestController中添加index方法
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
@GetMapping("/index")
public String index(){
return "index";
}
}
运行跳转页面,登录功能正常
用户注销
在配置类中添加退出设置
http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/hello").permitAll();
猜你喜欢
- 2024-09-29 Spring Security 自定义登录过程(非前后端分离版本)
- 2024-09-29 基于spring-security图形验证码、token验证
- 2024-09-29 10分钟上手SpringSecurity 框架(3)
- 2024-09-29 SpringBoot 实现自动登录时的安全风险控制
- 2024-09-29 springboot+security框架整合 springboot security详解
- 2024-09-29 时序图说明JWT用户认证及接口鉴权的细节
- 2024-09-29 Spring Security 整合OAuth2 springsecurity整合oauth2+jwt+vue
- 2024-09-29 有关springboot + spring security的思考
- 2024-09-29 SpringSecurity之自定义用户权限信息的存取
- 2024-09-29 你还不了解SpringSecurity吗?快来看看SpringSecurity实战总结
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)