权限管理

为什么要进行权限的管理?

让不同用户对不同资源具有不同控制权限,让资源安全性更高。

如何进行权限管理?

通过角色控制,不同用户有不同的角色,不同的角色具有不同的权限。

RBAC权限控制 Role Based Access Control 基于角色的权限控制

三个对象:

  1. 用户 具有不同的角色
  2. 角色 具有不同的权限
  3. 权限 权限可以由多个用户具有

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_ci

表设计:

  1. 用户表
  2. 角色表
  3. 用户角色表
  4. 权限表
  5. 角色权限表

SpringSecurity简介

安全框架,能够帮助实现权限控制,不同的用户能查看或操作不同的资源。

主流的安全框架:

  1. Shiro SSM配置少,容易上手
  2. SpringSecurity SpringBoot整合配置较少,强大

SpringSecurity是一个强大且高效的安全框架,能够提供用户验证和访问控制服务,能够很好地整合到以Spring为基础的项目中。
SpringBoot对SpringSecurity进行了大量的自动配置,使开发者通过少量的代码和配置就能完成很强大的验证和授权功能。

入门案例

引入spring security依赖后就会出现自带的登录效果:

  1. 相关依赖
    这里使用SpringBoot版本是2.4.4,SpringSecurity版本是5.4.5
<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.4.4</version>
     <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <dependencies>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>
  1. 测试页面
    在templates目录中添加页面:main.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Main</title>
</head>
<body>
    <h1>Welcome to Main</h1>
</body>
</html>
  1. 控制器
@Controller
public class UserController {

    @RequestMapping("/main")
    public String main(){
        return "main";
    }
 }
  1. 启动项目后,控制台会打印密码

    访问页面 http://localhost:8080/main 时,会出现自带的登录页面
    用户名默认为user,密码就是前面打印出来的

    登录成功后,看到main页面

自定义登录

项目中的登录功能肯定还是要自己开发,如何开发自己的登录功能呢?

  1. 定义登录页面login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--出现了验证错误后,实现下面内容-->
    <div th:if="${param.error}">
        <p style="text-align: center" class="text-danger">登录失败,账号或密码错误!</p>
    </div>
    <!--这里是提交给SpringSecurity配置的登录URL处理-->
    <form th:action="@{/login}" method="post">
    	<!--这里注意:名称必须是username和password,SpringSecurity默认指定的-->
        <input type="text" name="username" placeholder="Input your username"><br>
        <input type="password" name="password" placeholder="Input your password"><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>
  1. Controller添加方法
@RequestMapping("/login")
public String login(){
    return "login";
}
  1. Web验证的配置
/**
 * 启动Web安全验证
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 返回密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * 配置用户账号密码以及角色
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //在内存中创建用户
        auth.inMemoryAuthentication()
                //账号
                .withUser("admin")
                //密码,需要加密
                .password(new BCryptPasswordEncoder().encode("123"))
                //添加角色
                .roles("ADMIN","USER")
                //创建另一个用户
                .and()
                .withUser("user")
                .password(new BCryptPasswordEncoder().encode("123"))
                .roles("USER");
    }

    /**
     * 配置web页面的权限
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //用户请求授权
        http.authorizeRequests()
                //指定登录相关的请求,permitAll是不需要验证
                .antMatchers("/login").permitAll()
                //指定/user/** 需要USER角色
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/admin/**").hasRole("ADMIN")
                //其它所有URL都需要验证
                .anyRequest().authenticated()
                .and()
                //配置登录URL为login,登录成功后跳转main
                .formLogin().loginPage("/login").defaultSuccessUrl("/main")
                .and()
                //配置注销url,注销后到登录页面
                .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
    }
}
  1. 测试login,输入上面配置的账号和密码

登录成功

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_ci_02


账号密码填写错误

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring boot_03

密码处理

SpringSecurity登录验证使用的密码必须要经过加密处理,这里提供了PasswordEncoder接口进行密码加密。
PasswordEncoder接口提供两个主要方法:

  • String encode(CharSequence rawPassword)
    将原始密码加密,返回密文
  • boolean matches(CharSequence rawPassword,String password)
    将第一个参数原始密码和第二个参数密文进行匹配,返回是否匹配成功

PasswordEncoder的常用实现类是:BCryptPasswordEncoder
BCryptPasswordEncoder是基于hash算法的单向加密,可以控制密码强度,默认为10。
在上面的配置类中,配置了该加密器

@Bean
 public PasswordEncoder passwordEncoder(){
     return new BCryptPasswordEncoder();
 }

做下测试

@SpringBootTest
class SpringSecurityDbDemoApplicationTests {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Test
    void contextLoads() {
        for (int i = 0; i < 5; i++) {
            String encode = passwordEncoder.encode("123456");
            System.out.println("encode:"+encode);
            System.out.println("matches:"+passwordEncoder.matches("123456",encode));
        }}

}

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_html_04


可以看到同样是对"123456"进行加密,每次得到的密文都不相同,但是每次都可以匹配成功。

不同于另一个常用的安全框架:Shiro,SpringSecurity不需要给密码单独配置盐,盐是随机生成的,这样密码的安全性更高。

授权控制

在创建用户时,除了账号密码外,还可以添加对应的角色和权限,如:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         //在内存中创建用户
        auth.inMemoryAuthentication()
                //账号
                .withUser("admin")
                //密码,需要加密
                .password(new BCryptPasswordEncoder().encode("123"))
                //添加角色
                .roles("ADMIN","USER")
                //创建另一个用户
                .and()
                .withUser("user")
                .password(new BCryptPasswordEncoder().encode("123"))
                //也可以通过authorities添加权限和角色,如果是角色需要以ROLE_开头
                .authorities("LIST","ROLE_USER");
}

给指定的URL配置角色和权限,这样就可以进行访问控制了

@Override
protected void configure(HttpSecurity http) throws Exception {
    //用户请求授权
    http.authorizeRequests()
            //指定toLogin请求,permitAll不需要验证
            .antMatchers("/login").permitAll()
            //指定/user/** 需要USER角色
            .antMatchers("/user/**").hasRole("USER")
            .antMatchers("/admin/**").hasRole("ADMIN")
            //需要LIST权限
            .antMatchers("/admin/**").hasAuthority("LIST")
            //其它所有URL都需要验证
            .anyRequest().authenticated()
            .and()
            //配置登录页面为login,登录成功后跳转main
            .formLogin().loginPage("/login").defaultSuccessUrl("/main")
            .and()
            //配置注销url,注销后到登录页面
            .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
}

修改控制器的/main方法

@RequestMapping("/main")
public String main(Model model){
    //读取验证对象
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    //读取用户
    Object principal = authentication.getPrincipal();
    //如果是登录用户,则为org.springframework.security.core.userdetails.User
    if(principal instanceof User){
        User user = (User) principal;
        //读取用户名
        model.addAttribute("username",user.getUsername());
        //读取所有权限
        model.addAttribute("authorities",user.getAuthorities());
    }else {
        model.addAttribute("username", principal);
    }
    return "main";
}

修改main.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Main</title>
</head>
<body>
    <h1 th:text="|Welcome!${username}|">Welcome!</h1>
    你的权限有:
    <span th:each="auth:${authorities}">
        [[${auth}]]
    </span>
    <p><a href="/admin/admin">管理员页面</a> </p>
    <p><a href="/user/user">用户页面</a> </p>
    <p><a href="/login">登录页面</a> </p>

    <p>
        <form th:action="@{/logout}" method="post">
        <input type="submit" value="注销">
        </form>
    </p>
</body>
</html>

用admin登录,看到的权限是两个角色:ROLE_ADMIN和ROLE_USER,ROLE_是自动添加到角色上的。

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_java_05


用user登录

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring boot_06


再试一下访问不同的URL

添加目录和文件:admin/admin.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Admin</title>
</head>
<body>
    <h1 th:text="|Hello!${username},欢迎进入管理员页面|">Admin</h1>
</body>
</html>

user/user.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>User</title>
</head>
<body>
    <h1 th:text="|Hello!${username},欢迎进入用户页面|">User</h1>
</body>
</html>

错误页面:error/403.html,这里/error是Security默认的错误地址,添加/error/错误代码.html 后,出现对应错误时会自动跳转到对应页面,403是权限不足。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>403</title>
</head>
<body>
    Sorry!!你的权限不足
</body>
</html>

控制器添加方法:

@RequestMapping("/user/user")
 public String user(Model model){
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
     Object principal = authentication.getPrincipal();
     if(principal instanceof User){
         User user = (User) principal;
         model.addAttribute("username",user.getUsername());
         model.addAttribute("authorities",user.getAuthorities());
     }else {
         model.addAttribute("username", principal);
     }
     return "user/user";
 }

 @RequestMapping("/admin/admin")
 public String admin( Model model){
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
     Object principal = authentication.getPrincipal();
     if(principal instanceof User){
         User user = (User) principal;
         model.addAttribute("username",user.getUsername());
         model.addAttribute("authorities",user.getAuthorities());
     }else {
         model.addAttribute("username", principal);
     }
     return "admin/admin";
 }

用admin登录后,访问管理员的超链接,出现了权限不足

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring boot_07


访问用户超链接正常

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring boot_08


admin登录后只有ADMIN和USER角色,没有LIST权限,所以不能访问/admin/admin,可以修改创建admin用户时的授权配置,就可以访问了

//添加角色
// .roles("ADMIN","USER")
//添加ADMIN、USER角色和LIST权限,如果是角色需要以ROLE_开头
.authorities("LIST","ROLE_ADMIN","ROLE_USER")

总结

以上我们完成了SpringSecurity的基本登录验证功能,以及密码和授权的配置,真正开发时肯定是需要进行数据库验证的,下一章再说。

插入图片描述](https://s2.51cto.com/images/blog/202403/28214251_660573dbe4c3979736.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

访问用户超链接正常

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring boot_08


admin登录后只有ADMIN和USER角色,没有LIST权限,所以不能访问/admin/admin,可以修改创建admin用户时的授权配置,就可以访问了

//添加角色
// .roles("ADMIN","USER")
//添加ADMIN、USER角色和LIST权限,如果是角色需要以ROLE_开头
.authorities("LIST","ROLE_ADMIN","ROLE_USER")

自定义登录和授权逻辑

SpringSecurity的登录和授权逻辑可以通过实现UserDetailsService接口完成。
UserDetailsService接口:

public interface UserDetailsService {
    UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException;
}

loadUserByUsername方法通过用户名查询用户信息。
UserDetails接口,包含账号、密码和权限。

public interface UserDetails extends Serializable {
    Collection<? extends GrantedAuthority> getAuthorities();

    String getPassword();

    String getUsername();

    boolean isAccountNonExpired();

    boolean isAccountNonLocked();

    boolean isCredentialsNonExpired();

    boolean isEnabled();
}

UserDetails的主要实现类是:org.springframework.security.core.userdetails.User

案例整合

整合SpringBoot+SpringSecurity+MyBatis-Plus完成登录和授权
数据库采用RBAC(基于角色的权限控制)结构
用户和角色,角色和权限都是多对多关系
主要有5张表

  • user 用户表
  • role 角色表
  • permission 权限表
  • user_role 用户角色表
  • role_permission 角色权限表

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_html_10


用户密码采用BCryptPasswordEncoder进行了加密

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_java_11

项目依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
        <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blb_erp2?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456

mybatis-plus.type-aliases-package=com.xray.spring_security_db_demo.entity
mybatis-plus.mapper-locations=classpath:mapper/*.xml

实体类User、Role、Permission略
UserMapper接口
如果要进行用户授权,就需要通过用户名查询角色和权限

public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名查询所有角色
     */
    List<Role> selectRolesByUsername(String username);

    /**
     * 根据用户名查询所有权限
     */
    List<Permission> selectPermissionsByUsername(String username);
}

UserMapper.xml映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xray.spring_security_db_demo.mapper.UserMapper">

    <select id="selectRolesByUsername" resultType="Role">
        select r.* from user u join user_role ur on u.id = ur.user_id
        join role r on r.id = ur.role_id
        where u.username = #{username}
    </select>

    <select id="selectPermissionsByUsername" resultType="Permission">
        select p.* from user u join user_role ur on u.id = ur.user_id
        join role r on r.id = ur.role_id
        join role_permission rp on r.id = rp.role_id
        join permission p on p.id = rp.fun_id
        where u.username = #{username}
    </select>
</mapper>

实现UserDetailsService接口

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //根据用户名查询用户
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, s));
        if(user == null){
            throw new UsernameNotFoundException("Username is not exists");
        }
        StringBuilder authorities = new StringBuilder();
        //查询角色
        List<Role> roles = userMapper.selectRolesByUsername(s);
        //查询权限
        List<Permission> permissions = userMapper.selectPermissionsByUsername(s);
        //拼接角色到字符串中,角色需要以ROLE_开头
        roles.forEach(role -> authorities.append("ROLE_"+role.getName()+","));
        //拼接权限名称
        permissions.forEach(permission -> authorities.append(permission.getName()+","));
        authorities.deleteCharAt(authorities.length() - 1);
        //将用户名、密码以及所有角色和权限包装到userdetails.User对象中,返回
        org.springframework.security.core.userdetails.User user1 = new org.springframework.security.core.userdetails.User(user.getUsername(),
                user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(authorities.toString()));
        return user1;
    }
}

在配置类中,将内存中的用户改为数据库的自定义验证

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置数据库自定义验证
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //配置放行
                .antMatchers("/login").permitAll()
                //访问url需要某个权限
                .antMatchers("/user/**").hasAuthority("销售管理")
                //需要角色
                .antMatchers("/admin/**").hasRole("管理员")
                //除放行外,其他都要验证
                .anyRequest().authenticated()
                .and()
                //配置登录页面和登录后跳转的页面
                .formLogin().loginPage("/login").defaultSuccessUrl("/main")
                .and()
                //配置注销页面和注销后的页面
                .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
    }
}

启动类

@MapperScan("com.xray.spring_security_db_demo.mapper")
@SpringBootApplication
public class SpringSecurityDbDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringSecurityDbDemoApplication.class, args);
    }

}

UserController控制器不变

启动项目,使用admin登录

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_ci_12


可以看到所有的角色和权限

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_java_13


admin具有管理员角色和销售管理权限,可以进入管理员和用户页面

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring_14


springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring_15


另一个用户heng有销售管理权限,没有管理员角色,不能进入管理员页面

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_ci_16


但可以访问用户页面

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring_17

实现RememberMe

可以在前面的案例中加入记住我的功能
登录页面添加复选框,name为rememberMe

<form th:action="@{/login}" method="post">
        <input type="text" name="username" placeholder="Input your username"><br>
        <input type="password" name="password" placeholder="Input your password"><br>
        <input type="checkbox" name="rememberMe" value="true">记住我<br>
        <input type="submit" value="Login">
    </form>

添加RememberMe的配置,此配置主要是在MySQL数据库中创建RememberMe相关的表,并返回该表的jdbc操作对象。

@Configuration
public class RememberMeConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        //在第一次启动时建表,后面需要关闭此配置,否则会出错
//        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }
}

修改配置类SecurityConfig
注入jdbc的操作对象

@Autowired
private PersistentTokenRepository persistentTokenRepository;

在configure方法中加入RememberMe的配置

http
//配置记住我
.rememberMe()
//表单中的名称
.rememberMeParameter("rememberMe")
//jdbc操作对象
.tokenRepository(persistentTokenRepository)
//记住我的时间为60秒
.tokenValiditySeconds(60);

登录的用户勾选记住我,关闭页面后在60秒内可以直接进入后台,60秒后需要重新登录

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_ci_18


在数据库中会出现persistent_logins表,会记录每个用户的登录时间

springsecurity实现用户角色权限管理和菜单管理 spring security权限表和角色表_spring_19

CSRF

跨站请求攻击

解决方案:

1、启动csrf防御(不加csrf().disable())

2、表单加:

<input type="hidden" th:name = "${_csrf.parameterName}" th:value = "${_csrf.token}" />

总结

以上我们完成了SpringSecurity的基本登录验证功能,以及密码和授权的配置,真正开发时肯定是需要进行数据库验证的,下一章再说。