普歌 - SpringSecurity入门(1)


前言

提示:学习springSecurity需要具备springboot,maven等基础性知识


一、项目结构

模块名说明
puge-security-parent父模块,pom 类型,进行统一的版本管理,聚合管理子模块
puge-security-base基础通用功能管理, 如工具类
puge-security-core进行安全管理, 实现身份认证、验证码认证、手机登录、用户授权等
puge-security-webWeb 业务应用, thymeleaf dao service controller

二、使用maven创建项目

1.创建项目

项目结构图

parent的pom文件:

代码如下(示例):

    <description>聚合工程 来耦合其它工程</description>
    <!--修改打包方式  因为为聚合工程 所以为pom文件-->
    <packaging>pom</packaging>


    <!-- Spring Boot 父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
<!--        <relativePath/>-->
    </parent>

    <!-- 依赖版本号 -->
    <properties>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
        <druid.version>1.1.12</druid.version>
        <oauth2-autoconfigure.version>2.1.3.RELEASE</oauth2-autoconfigure.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <fastjson.version>1.2.8</fastjson.version>
        <commons-lang.version>2.6</commons-lang.version>
        <commons-collections.version>3.2.2</commons-collections.version>
        <commons-io.version>2.6</commons-io.version>
        <!-- 定义版本号, 子模块直接引用-->
        <puge-security.version>1.0-SNAPSHOT</puge-security.version>
    </properties>

    <!-- 集中式管理依赖版本号,并没有真实依赖 -->
    <dependencyManagement>
        <dependencies>
            <!--mybatis-plus启动器-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!--spring-security-oauth2、spring-security-jwt等-->
            <dependency>
                <groupId>org.springframework.security.oauth.boot</groupId>
                <artifactId>spring-security-oauth2-autoconfigure</artifactId>
                <version>${oauth2-autoconfigure.version}</version>
            </dependency>
            <!-- kaptcha 用于图形验证码 -->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- 工具类依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>${commons-lang.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons-collections.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

<!--    打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

base的pom文件:

代码如下(示例):

  <description>基础公共依赖工程</description>
    <artifactId>puge-security-base</artifactId>

    <dependencies>
        <!--类中setter/getter,使用注解-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 工具类依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
    </dependencies>

core的pom文件:

代码如下(示例):

  <artifactId>puge-security-core</artifactId>
    <description>本工程提供安全授权服务--安全管理模块</description>

    <dependencies>
        <!--依赖mengxuegu-security-base基础模块 -->
        <dependency>
            <groupId>pro.puge</groupId>
            <artifactId>puge-security-base</artifactId>
            <version>${puge-security.version}</version>
        </dependency>

      <!-- spring security 启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
     <!--springSecurity 过滤链依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>


        <!--数据库依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

web的pom文件:

代码如下(示例):

 <artifactId>puge-security-web</artifactId>
    <description>web页面工程-实际启动模块</description>

    <dependencies>
        <!-- web启动器, 对springmvc, servlet等支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--权限核心模块, 注意:要放到 web启动器下面-->
        <dependency>
            <groupId>pro.puge</groupId>
            <artifactId>mengxuegupuge-security-core</artifactId>
            <version>${puge-security.version}</version>
        </dependency>
        <!-- thymeleaf 模块启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--对Thymeleaf添加Spring Security标签支持-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        
        <!--mybatis-plus启动器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>


        <!-- application.yml 配置处理器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- springboot 单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

初级使用

配置和导入security启动包

提示:启动配置在web中,启动包在core中

server:
  port: 80

spring:
  thymeleaf:
    cache: false #关闭thymeleaf缓存
<!--         spring security 启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

准备好一组页面

提示:准备好后导入到web项目中的resource中
在这里插入图片描述

编写跳转首页控制器

/**
 * @author LIJW
 * @date 2021/3/26 11:15 下午
 * 本类作用是配置跳转首页
 */
public class mainController {
    /**
     * 加了中括号可以多个路径匹配
     * @return
     */
    @RequestMapping({"/index","/",""})
    public String index(){
        return "index";
    }
}

在core模块中创建安全配置类(先使用httpBasic方式认证,不写@Configuration,让配置类暂时不生效,先试用security默认提供的用户名和密码)

提示:需要继承WebSecurityConfigurerAdapter并重写AuthenticationManagerBuilder和HttpSecurity


/**
 * @author LIJW
 * @date 2021/3/27 9:38 上午
 *  security 安全配置类
 *  继承于webSecurityConfigurerAdapter抽象类
 */
public class springSecurity  extends WebSecurityConfigurerAdapter {


    /**
     * 重写configure(AuthenticationManagerBuilder)身份认证管理器
     * 1.认证信息提供方式(用户名、密码、当前用户的资源权
     * 2.可采用内存存储方式,也可能采用数据库方式等
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
    }

    /**
     * configure(HttpSecurity http) 资源权限配置(过滤器链)
     * 1.拦截的哪一些资源
     * 2.资源所对应的角色权限
     * 3.定制登录页面、登录请求地址、错误处理方式
     * 4.自定义 spring security 过滤器等
     * 5.定义认证方式:httpBasic httpForm
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http.httpBasic()
               .and()  //链接符号
               .authorizeRequests() //认证请求
               .anyRequest().authenticated() //所有进入应用的HTTP请求都要进行认证
            ;
    }
}

在web模块中创建启动类

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//@SpringBootApplication
public class webApplication {
    public static void main(String[] args) {
        SpringApplication.run(webApplication.class,args);
    }
}

测试

启动项目,因为没有设置密码和用户名,使用的是内存存储方式,所以暂时使用springSecurity提供默认密码和用户名
默认密码

默认:
用户名:user
密码: 控制台打印
默认提供登录页面
认证成功
在这里插入图片描述


喜欢的小伙伴可以点赞、关注、收藏哦

– 技术源于追求,技术改变生活 –


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值