3.Spring Boot + Security初步使用(覆盖配置的两种方式 配置类和XML注入以自定义页面为例)

2023-10-26

1.Spring Security简介

Spring Security 是一个基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。

2.使用Spring Boot 的Security

1.在pom.xml 中添加

<!--添加Spring Security的Spring Boot 默认依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.重启应用

这个时候访问应用你就会发现

我没有配置用户名密码呀,那么它的用户名和密码是什么呢?
原来这里Spring Boot 又发挥了它的自动配置,他给你设置了默认的用户名和密码,用户名默认是user 密码是在你的启动日志中,你的启动日志里会有下面这个东西,这就是你的密码

这里写图片描述

到这里你可能会感觉这么简单, 但是有没有感觉很不爽, 大哥你给的这是什么玩意儿, 界面就一个弹出框难看的一逼也就算了, 用户名密码都不能自己搞, 也太难用了点.
好吧要定义自己的用户名密码,这个时候通过application.properties来配置

security.user.name=user # 默认用户名
security.user.password=123456 # 默认用户名密码

但是我想要一个炫酷的验证界面怎么办?这个时候就要覆盖Spring Boot的自动配置了

2.覆盖Spring Boot的security(两种方式)

本次代码依赖于第一章的程序
这次只是做一个登陆的简单案例,具体的会在接下来的讲.
针对自定义有两种方案, 一种是基于Spring Boot的重写覆盖,另一种是通过XML来覆盖配置.

2.1先看第一种方法

2.1.1首先写一个类来继承WebSecurityConfigurerAdapter
package com.hand.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * Created by JQY on 2017/11/21.
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/","/styles/**","/scripts/**").permitAll() //指定那些URL不要被保护
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login") //登录的时候你要跳转到哪里
                .failureUrl("/login?error") //失败页面
                .permitAll() //登录任意访问
                .and()
                .rememberMe() //rememberMe
                .and() //注销行为任意访问
                .logout()
                .permitAll()
                .and()
                .csrf() //关闭csrf 不然不支持post
                .disable();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                //用户名密码和角色
                .withUser("jyq").password("123456").roles("USER");
    }
}
2.1.2上面写了跳向login, 接下来写login的controller
package com.hand.sys.controller;

import com.hand.demo.model.ResouceTable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by JQY on 2017/11/21.
 */
@Controller
public class LoginController
{
    @RequestMapping("login")
    public String login(Model model)
    {
        return "/sys/login";
    }
}
2.1.3接下来麻烦一点了,要写一个炫酷的界面

要炫酷就要把前端的样式表加上
拷一个页面过来,同时拷贝样式表和js
样式表和CSShttps://github.com/masterjyq/Spring-Boot.git
页面和样式表的位置如下:
这里写图片描述
页面代码修改form的action, 和input 的name 最终如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="Appboard - Admin Template with Angularjs">
    <meta name="keywords" content="appboard, webapp, admin, dashboard, template, ui">
    <meta name="author" content="solutionportal">
    <!-- <base href="/"> -->

    <title>Appboard - Admin Template</title>

    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800' rel='stylesheet' type='text/css'/>

    <!-- Icons -->
    <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css"/>

    <!-- Set this in dist folder in index.html file -->
    <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/bootstrap-3.3.4.css"/>
    <link rel="stylesheet" th:href="@{{path}/styles/main.min.css(path=${contextPath})}"/>

    <!-- Match Media polyfill for IE9 -->
    <!--[if IE 9]><!-->
    <script type="application/javascript" th:src="@{{path}/scripts/ie/matchMedia.js(path=${contextPath})}"></script>
    <!--<![endif]-->

</head>

<body ng-app="app" id="app" class="app {{themeActive}}" custom-page ng-controller="AppCtrl">

<div class="page page-auth clearfix">

    <div class="auth-container">
        <!-- site logo -->
        <h1 class="site-logo h2 mb15"><a href="/"><span>App</span>&nbsp;Board</a></h1>
        <h3 class="text-normal h4 text-center">Sign in to your account</h3>

        <div class="form-container">
            <form class="form-horizontal" th:action="@{/login}" method="post">
                <div class="form-group form-group-lg">
                    <input name="username" class="form-control" type="text" placeholder="用户名" value="">
                </div>

                <div class="form-group form-group-lg">
                    <input name="password" class="form-control" type="password" placeholder="密码">
                </div>
                <div class="clearfix"><a href="#/pages/forget-pass" class="right small">忘记密码</a>
                </div>
                <div class="clearfix mb15">
                    <button type="submit" class="btn btn-lg btn-w120 btn-primary text-uppercase">登录</button>
                    <div class="ui-checkbox ui-checkbox-primary mt15 right">
                        <label>
                            <input type="checkbox" id="keep-login" name="remember-me">
                            <span>Remember me</span>
                        </label>
                    </div>
                </div>

                <div class="clearfix text-center">
                    <p>Don't have an account? <a href="#/pages/register">Register Now</a>
                    </p>
                </div>
            </form>
            <div th:if="${param.error}">
                <span style="color: red">用户名或密码错误</span>
            </div>
            <div th:if="${param.logout}">
                <span style="color: red">您已注销成功</span>
            </div>
        </div>

    </div>
    <!-- ##end auth-wrap -->
</div>
<!-- Set this in dist index.html -->
<script type="application/javascript"  th:src="@{{path}/scripts/vendors.js(path=${contextPath})}"></script>
<script type="application/javascript" th:src="@{{path}/scripts/plugins.js(path=${contextPath})}"></script>
<script type="application/javascript" th:src="@{{path}/scripts/app.js(path=${contextPath})}"></script>

<!-- !End -->
</body>

</html>

弄好了先测试一下页面, 有错误,
这里写图片描述

是 thymeleaf强制校验页面元素的原因,不想修改页面就去掉强制校验

pom.xml里添加

<!--取消thymeleaf对页面的强制校验-->
<dependency>
   <groupId>net.sourceforge.nekohtml</groupId>
   <artifactId>nekohtml</artifactId>
   <version>1.9.22</version>
</dependency>

在application.properties里修改 thymeleaf配置文件如下

#spring.thymeleaf.mode=HTML5

#取消thymeleaf对页面的强制校验
spring.thymeleaf.mode=LEGACYHTML5

现在你来访问login就成功了

这里写图片描述

接下来在第一章节做resource_table.html 中加一个登出的按钮

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

到这里使用配置类实现的自定义界面就完成了

2.1.4到这里就成了,现在再次访问之第一章节的http://localhost:8080/res/query

这里写图片描述

就会自动跳转到登录界面,输入你配置的 jyq/123456 ,就到我们第一章做的那个页面了

这里写图片描述

点击一下注销就会回到登录页面

这里写图片描述

2.2 接下来使用XML方式实现上述的功能

使用XML的配置和刚刚基本一样,就是把配置类换成XML配置文件
既然要使用XML那么就要干掉刚刚的配置类了,不用删除, 直接把注解去除就行了, 这样就不会扫描到这个类添加到配置里面了

这里写图片描述

不用配置类那么你就要写一个配置文件了.位置如下

这里写图片描述

代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-4.2.xsd">

    <!--拦截器过滤配置-->
    <sec:http auto-config="true">
        <!--开启remember-me-->
        <sec:remember-me />
        <!--关闭csrf-->
        <sec:csrf disabled="true"/>
        <!--静态资源的URL-->
        <sec:intercept-url pattern="/scripts/**" access="hasRole('ROLE_ANONYMOUS')"/>
        <sec:intercept-url pattern="/styles/**" access="hasRole('ROLE_ANONYMOUS')"/>

        <!--登录界面不要拦截-->
        <sec:intercept-url pattern="/login" access="hasRole('ROLE_ANONYMOUS')"/>

        <!--具体页面拦截规则-->
        <sec:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>

        <!--自定义页面-->
        <sec:form-login login-page="/login"   authentication-failure-url="/login?error"
                        default-target-url="/login"/>

    </sec:http>

    <!--配置用户-->
    <sec:authentication-manager>
        <sec:authentication-provider>
            <sec:user-service >
                <sec:user name="jyq" password="123456" authorities="ROLE_ADMIN"/>
            </sec:user-service>
        </sec:authentication-provider>
    </sec:authentication-manager>
</beans>

配置文件写完你就应该想了, Spring Boot 里面没有web.xml 怎么把读取配置文件注入Bean呢? Spring Boot 给了我们一种另外的xml注入的方式. 这个时候只要在应用的引导类,就是本应用的DemoApplication里使用 @ImportResource 来解决,如下

package com.hand;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations={"classpath:applicationContext-security.xml"})
public class DemoApplication {

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

好了到这里你就成功的干掉了配置类,使用xml来配置. 至于controller ,页面这些和配置类一样,不用修改.
可以在重启应用试试了.

到这里, 用户名密码还是我们写在配置文件里,要想从数据库中读取怎么办呢? 要想实现用户,角色的控制全部在数据库里维护该怎么做呢? 下面的将会探讨这些.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

3.Spring Boot + Security初步使用(覆盖配置的两种方式 配置类和XML注入以自定义页面为例) 的相关文章

随机推荐

  • 第09课:生活中的工厂模式——你要拿铁还是摩卡

    用程序来模拟生活 从剧情中思考工厂模式 工厂模式的模型抽象 类图 模型说明 简单工厂的优点 简单工厂的缺点 模型的拓展应用 应用场景 拓展 工厂三姐妹 简单工厂模式 工厂方法模式 抽象工厂模式 进一步思考
  • 遇到问题--Nginx---tomcat启动web程序访问静态资源时404找不到

    http blog csdn net zzq900503 article details 76927074 给web站点配置域名转发后 tomcat启动web程序访问静态资源时404找不到 经过确认项目资源路径都没问题 后来经过排查后发现是
  • 配置samba服务

    什么是samba服务 是 和windows 进行 文件打印机共享的组件 结果就是linux windows 之间可以互相访问它们的共享文件 说明 我用的是ubuntu系统 ubuntu系统安装samba服务 确定自己是否安装samba dp
  • (一)Google发布了一个新的Tensorflow物体识别API

    做图像识别有很多不同的途径 谷歌最近发布了一个使用Tensorflow的物体识别API 让计算机视觉在各方面都更进了一步 这篇文章将带你测试这个新的API 并且把它应用在youtube上 可以在GitHub上获取用到的全部代码 https
  • JDBC连接数据库URL示例

    jdbc mysql localhost 3306 crm useSSL false useUnicode true characterEncoding UTF 8
  • matlab扩充内存,matlab中内存不够用的解决方案

    1 在命令行中输入pack函数来整理内存 pack函数到底是什么机制呢 这里参考了MATLAB的help文档 话说回来 help始终是学习MATLAB的 金参考标准 用法 pack pack filename pack filename 第
  • Struts配置文件详解

    Struts1配置文件
  • 最好的工程师像投资者一样思考,而不是建设者

    我在大学期间住在图书馆 我学习的教科书理论越多 我就会成为一名更好的工程师 我想 然而 当我开始工作时 我注意到业内最优秀的工程师并不一定比应届毕业生了解更多的理论 他们只是带来了不同的心态 即投资者的心态 正是这种心态帮助他们提出更聪明的
  • 谈一谈,自身对技术经理这个职位的理解吧

    前言 19年初在上一家公司离职 在上一公司服役了4年半 成长了不少 收获了不少东西 在上一公司也带过很多团队 多的时候6 7个人 少的时候2人 也总结了很多的所谓的经验吧 由于一系列原因吧 离职来到了我现在的公司 岗位职责 到这边以后 入职
  • 什么是MyBatis

    一 MyBatis概述 1 1 原始的JDBC操作 谈及mybatis 必然需要先了解Java和数据库的连接技术 JDBC Java DataBase Connectivity 但是原始JDBC操作中 却存在如下缺点 数据库连接创建 释放频
  • 计算机制作节日贺卡教案,《制作节日贺卡》教学设计.doc

    文档介绍 制作节日贺卡 教学设计 小学信息技术 第五册第1单元第3课吴信平肥西县三河镇三河学区中心学校电子邮箱 ahicon 电话 教材分析本课是制作电子贺卡单元的第3课 教材内容为指导学生完整地制作一张贺卡 让学生学会如何制作一个简单的电
  • Spark是否能替代Hive

    在实际生产环境中已经形成了离线以Hive为主 Spark为辅 实时处理用Flink的大数据架构体系及Impala Es Kylin等应用查询引擎 但是有很多学习Spark的程序员普遍认为Spark必然会替代Hive成为新的一代大数据仓库标准
  • Linux设备驱动开发入门之——hello驱动

    1 Linux驱动程序的分类 Linux 中主要分为三大类驱动 字符设备驱动 块设备驱动和网络设备驱动 1 字符设备驱动 因为软件操作设备是是以字节为单位进行的 是按照字节流进行读写操作的一种设备 典型的如LCD 蜂鸣器 SPI 触摸屏等驱
  • dfs 遍历二叉树

    dfs 遍历二叉树 为了更好的理解dfs 手写了dfs 遍历二叉树的两种方式 方法 一种是采用常用的递归执行 另一种是采用循环执行 使用栈来代替递归 二叉树定义 class Node get set方法省略 private Node lef
  • ZF预编码,R-ZF预编码,脏纸预编码(DPC预编码)

    如何理解通信中的迫零预编码 正则化迫零预编码与脏纸预编码 ZF预编码可以理解为把user自身因为多天线空间信道传输而产生的在同时同频的自干扰消掉 但没有考虑消去高斯分布的noise 从而最小化了真实信号和估计的误差平方和 而从统计来看就是M
  • 从零开始研究Gitlab(利用vscode进行同步)

    1 vscode添加远程gitlab仓库 这个一步步按着做就好了 最开始的ssh秘钥创建可以直接在gitlab页面里点击 获取更多 查看步骤 https jingyan baidu com article afd8f4de9dcad475e
  • os.walk和os.listdir

    1 os walk 可以一级一级循环低递归到最下面的一级目录 for root img names in os walk img dir for img name in img names img path os path join roo
  • 搭建iOS开发环境

    1 准备 当前移动开发主要分为安卓开发和iOS开发 安卓是谷歌在2007年宣布开源的移动操作系统 iOS是苹果2007年发布的 两个系统出现的时间大致相同 在随后的十年间引领了移动开发浪潮 如今各种移动开发技术层出不穷 一些跨平台的技术也相
  • 程序流图画法详解

    程序流图一般是软件评测师考试中的第一道大题 同时也是必考大题 多层嵌套的循环程序绘制流程图时十分繁琐 本人在经过练习真题以及查阅资料后有了一些绘制控制流图的小经验 如有不对请指出 下面以2017年的软件评测师下午第一套真题为例进行讲解 1
  • 3.Spring Boot + Security初步使用(覆盖配置的两种方式 配置类和XML注入以自定义页面为例)

    1 Spring Security简介 Spring Security 是一个基于 Spring 框架 提供了一套 Web 应用安全性的完整解决方案 一般来说 Web 应用的安全性包括用户认证 Authentication 和用户授权 Au