SpringSecurity安全授权,转疯了

本文详细介绍了SpringSecurity的认证流程,包括内存认证和基于数据库的登录验证。通过配置WebSecurityConfigurerAdapter,展示了如何使用BCryptPasswordEncoder进行密码加密。还涉及到UserDetailsService接口,用于从数据库加载用户数据。此外,提到了如何使用HttpSecurity进行URL路径的权限控制,并给出了创建数据库表结构和使用MyBatis进行用户角色查询的示例。
摘要由CSDN通过智能技术生成

public class RealSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
PasswordEncoder passwordEncoder(){
//NoOpPasswordEncoder 在高版本的Spring Boot 中已被弃用,不建议使用
return new BCryptPasswordEncoder();
}

@Autowired PasswordEncoder passwordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)//使用对应的加密
.withUser(“admin”)
.password(passwordEncoder.encode(“123456”))//使用对应的解密
.roles(“ADMIN”,“USER”)
.and()
.withUser(“freephp”)
.password(passwordEncoder.encode(“123456”))
.roles(“USER”);
}
}

上面这段代码中,inMemoryAuthentication 代表把这个配置保存在内存中,然后使用 withUser 方法增加授权账号,用 password 方法设置密码,用 roles 来设置账号所属的权限群组。

HttpSecurity 是另外一种认证方式,也是使用 configure 方法,具体代码如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(“/admin/“)
.hasRole(“ADMIN”)
.antMatchers(”/user/
”)
.access(“hasAnyRole(‘ADMIN’,‘USER’)”)
.anyRequest()
.authenticated()//任意登录的用户都可以访问
.and()
.formLogin()
.loginProcessingUrl(“/login”)
.permitAll()
.and()
.csrf()
.disable();
}

使用 andMatcher 设置需要被授权的 URL 路径,access 方法给予某些角色访问权限,代码如下:。

package org.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SwagController {
@GetMapping(“/user/sayHi”)
public String myUser(){
return “Hi,user”;
}

@GetMapping(“/admin/hello”)
public String admin(){
return “admin page”;
}

@GetMapping(“/hello”)
public String hello(){
return “hello,man”;
}
}

运行项目后,访问 http://localhost:8080/admin/hello, 则会要求输入账号和密码,使用 admin 账号,密码输入 123456,即可进入后台 /admin/hello 页面,如图。

5.认证流程

  • Authentication接口: 它的实现类,表示当前访问系统的用户,封装了用户相关信息。
  • AuthenticationManager接口: 定义了认证Authentication的方法。
  • UserDetailsService接口: 加载用户特定数据的核心接口。里面定义了一个根据用户名查询用户信息的方法。
  • UserDetails接口: 提供核心用户信息。通过UserDetailsService根据用户名获取处理的用户信息要封装成UserDetails对象返回,然后将这些信息封装到Authentication对象中。

所以接下来如果要使用数据库做登录验证,只要把实现类 InMemoryUserDetailsManager 做一个替换,DaoAuthenticationProvider 调用自定义的实现类。让其不再使用内存做认证进入数据库查询,实现 UserDetailsService 接口即可。

6.基于数据库查询的登录验证

之前都是使用内存来存储认证数据,其实可以考虑使用数据库进行持久化数据存储。这样更加方便进行账号管理,也更符合实际项目开发的需求。

创建一个 roles 库,然后创建用户表、角色权限表、用户和角色权限关系表。

create database r_security;
use r_security;
– 用户表
CREATE TABLE r_users(
id int(11) unsigned NOT NULL AUTO_INCREMENT primary key COMMENT ‘主键’,
username varchar(50) not null comment ‘账号名’,
password varchar(300) not null comment ‘密码’,
status tinyint(11) not null comment ‘账号状态,1:正常、2:被封’,
created int (11) not null comment ‘创建时间,时间戳’
);
– 角色权限表
create table r_roles(
id int(11) unsigned not null auto_increment comment ‘主键’,
name varchar(50) not null comment ‘角色名’,
permission_path varchar(500) not null comment ‘权限路径,如/admin/*’,
primary key (id)
);
– 用户角色权限关系表
create table r_user_roles(
id int(11) unsigned not null auto_incr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值