springboot+security整合1

说明springboot版本2.0.3

一、 介绍

  Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

二、 环境搭建

  建立springboot2项目,加入security依赖,mybatis依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>

数据库为传统的用户--角色--权限,权限表记录了url和method,springboot配置文件如下:

mybatis: type-aliases-package: com.example.demo.entity server: port: 8081 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: 123456 http: encoding: charset: utf-8 enabled: true

springboot启动类中加入如下代码,设置路由匹配规则。

@Override protected void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(false) //设置路由是否后缀匹配,譬如/user能够匹配/user.,/user.aa .setUseTrailingSlashMatch(false); //设置是否后缀路径匹配,比如/user能够匹配/user,/user/ } 三、 security配置

  默认情况下security是无需任何自定义配置就可使用的,我们不考虑这种方式,直接讲如何个性化登录过程。

1、 建立security配置文件,目前配置文件中还没有任何配置。 @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { } 2、 个性化登录,security中的登录如下:

img

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwsgs.html