Spring Security 整合 thymeleaf 实现动态权限 (2)项目基本配置

项目基本配置

创建一个基本的springboot项目。基本依赖如下
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis.spring.boot.starter.version}
        
        
        
            mysql
            mysql-connector-java
            runtime
        
application.yml配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/security?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: 你的数据库密码
  thymeleaf:
    #是否缓存
    cache: false
    # 在构建URL时预先查看名称的前缀
    prefix: classpath:/templates/
    # 构建URL时附加查看名称的后缀
    suffix: .html
  #mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
创建实体类以及基本的mapper,这里使用easycode自动生成。
并对User实体类进行相应的改造,
/**
 * 系统用户(User)实体类
 *
 * @author sdl
 * @since 2020-03-22 15:15:14
 */
public class User implements UserDetails, Serializable {
    private static final long serialVersionUID = -52166883900608909L;
    /**
    * ID
    */
    private Integer id;
    /**
    * 用户名
    */
    private String username;
    /**
    * 密码
    */
    private String password;
    
    其他属性省略...
    Getter and Setter省略...
    private List roles;

   //UserDetails的角色资源属性集合
    @Override
    public Collection getAuthorities() {
        List authorities = new ArrayList<>(roles.size());
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }
   // 账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    // 账号是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    // 账号凭证是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    // 账号是否可用,数据库中定义了enabled字段
    @Override
    public boolean isEnabled() {
        return enabled;
    }
以及相应的dao层方法和mapper

dao

Spring Security 整合 thymeleaf 实现动态权限 (2)项目基本配置_第1张图片

你可能感兴趣的:(springboot,springsecurity,thymeleaf)