SpringBoot—Security安全管理

SpringBoot—Security安全管理

Spring Security 是 Spring 家族中的一个安全管理框架,Spring Boot 对于 Spring Security 提供了 自动化配置方案,可以零配置使用 Spring Security。

SpringBoot 2.x版本

1.整合Security。

2.简单示例

3.详细配置。

一、整合Security

1.加入Security依赖,为了方便演示,加入thymeleaf依赖。

 	<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>

2.创建Controller类,在里面写个方法。

@Controller
public class SecurityController {
    @RequestMapping("/hello")
    public String Hello(Model model){
        model.addAttribute("msg","Spring Security!!!");
        return "Hello";
    }
}

3.创建thymeleaf页面,Hello.html.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Security安全管理哦</title>
</head>
<body>
<h1>Security管理中</h1>
<div>
    <h3 th:utext="${msg}"></h3>
</div>
</body>
</html>

4.在properties中设置好访问端口后,此时Security模块已经开启了。启动项目,控制台会自动给出一个密码,默认用户名为user,此时访问Controller中的方法,浏览器出现以下界面,输入用户名和密码,才能进入Hello.html页面。

在这里插入图片描述

二、简单示例

接下来我们自己写一个Security的配置类,简单了解一下Security的使用。

1.在template下创建一些用来演示的html页面。

在这里插入图片描述

2.把Hello.html略作修改。

<body>
<h1>Security管理中</h1>
<div>
    <h3 th:utext="${msg}"></h3>
</div>
<div>
   <div>
        <h1>初级技能</h1>
        <a th:href="@{level_1/1}">加减乘除</a>
    </div>

    <div>
        <h1>中级技能</h1>
        <a th:href="@{level_2/1}">函数方程</a>
    </div>
</div>
</body>

3.创建Security的配置类,该配置类需要继承 WebSecurityConfigurerAdapter ,并重写配置方法。这里重写了认定和授权方法。

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/hello").permitAll()
                .antMatchers("/level_1/1").hasRole("Lv_1")
            .antMatchers("/level_2/1").hasRole("Lv_2");
       
        http.formLogin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        auth.inMemoryAuthentication().passwordEncoder(new 				                                          BCryptPasswordEncoder()).withUser("Jack")
                .password(new BCryptPasswordEncoder().encode("123456")).roles("Lv_1");
    }


}

1)、这里的代码有些复杂,加密了密码算法,如果不写登录会报错,后面我们可以单独设置方法来解决。

auth.inMemoryAuthentication().passwordEncoder(new 				                                          BCryptPasswordEncoder()).withUser("Jack")
                .password(new BCryptPasswordEncoder().encode("123456")).roles("Lv_1");

SpringBoot2.0抛弃了原来的NoOpPasswordEncoder,要求用户保存的密码必须要使用加密算法后存储,在登录验证的时候Security会将获得的密码在进行编码后再和数据库中加密后的密码进行对比

这里也可以在配置类中使用原来的这个方法,当然不推荐,因为已经被废弃了,老版本适用,新版本会报错。

@Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }

2)、或者用 BCryptPasswordEncoder加密,设置一个bean,但是此时的密码还是明文,所以密码部分还是需要设置编码。

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

此时1)部分的代码可以如下

auth.inMemoryAuthentication().withUser("Jack")
                .password(new BCryptPasswordEncoder().encode("123456")).roles("Lv_1")

4.在Controller中继续加入两个路由。

 @RequestMapping("/level_1/1")
    public String level_1(){

        return "level_1/1";
    }
    @RequestMapping("/level_2/1")
    public String level_2(){

        return "level_2/1";
    }

5.此时我们访问hello,进入以下界面。

在这里插入图片描述

点击加减乘除,此时需要表单验证,输入Jack和密码123456,进入level_1/1.html,返回,点击函数方程,出现以下界面。

在这里插入图片描述

因为Lv_1角色无法访问这个界面,Lv_2角色才可以进入。

三、详细配置

3.1、自定义配置类

自定义配置类需要继承WebSecurityConfigurerAdapter,开启@EnableWebSecurity注解。

重写configure(HttpSecurity http)和configure(AuthenticationManagerBuilder auth)方法可以自定义规则。

1)、http.authorizeRequests()方法开启授权路径,antMatchers("/hello").permitAll()代表/hello路径所有人都可以访问,.antMatchers("/level_1/*").hasRole(“Lv_1”)代表/level_1/下路径只有Lv_1角色才可以访问。

2)、auth.inMemoryAuthentication().withUser("Jack") .password(new BCryptPasswordEncoder().encode("123456")).roles("Lv_1")代表在内存中设置用户,密码及其角色。

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/hello").permitAll()
                .antMatchers("/level_1/*").hasRole("Lv_1")
                .antMatchers("/level_2/*").hasRole("Lv_2")
                .antMatchers("/level_3/*").hasRole("Lv_3")
        ;


        http.formLogin().passwordParameter("pwd").usernameParameter("username")
                .loginPage("/userLogin")
                .successForwardUrl("/hello");
        http.logout().logoutSuccessUrl("/hello");

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        auth.inMemoryAuthentication().withUser("Jack")
                .password(new BCryptPasswordEncoder().encode("123456")).roles("Lv_1")
                .and().withUser("Sally").password(new BCryptPasswordEncoder().encode("999"))
                .roles("Lv_3","Lv_2","Lv_1");
    }

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

3.2、自定义登录页
  http.formLogin().passwordParameter("pwd").usernameParameter("username")
                .loginPage("/userLogin").loginProcessingUrl("/userLogin")
                .successForwardUrl("/hello");
        http.logout().logoutSuccessUrl("/hello");

这里设置了/userLogin路径为登录路径,SpringBoot Security中登录是需要提交一个Post表单的,这里设置了表单参数为pwd和username,表单提交路径为loginProcessingUrl("/userLogin"),登录成功后返回/hello,

3.3、开启Thymleaf对Security的支持

Thymleaf对Security提供了一些支持标签,方便操作。

如下 sec:authorize="!isAuthenticated()",如果没有登陆,标签里面的内容不会显示。除此之外,还有许多其他的标签。

<div sec:authorize="!isAuthenticated()">
    <h3>你好,请登录</h3>
    <a th:href="@{/userLogin}">登录</a>
</div>

使用支持需要引入依赖,同时在html页面引入命名空间。

<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"

这里的版本冲突需要注意。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## 介绍 使用 spring boot,基于 ssm 框架和 shiro 安全框架,开发的一个物流管理系统。前端使用的是 H-ui 开源框架,运用了 Bootstrap table、zTree、PageHelper、jQuery validate 等插件。 ## 特点 1. 基于 spring boot、maven 构建; 2. 运用了 springspring mvc、mybatis 和 shiro 框架; 3. 采用了 RBAC 的思路设计系统,采用了 Mysql 作为数据库支持; 4. 不同的角色登录系统会有不同的菜单列表,且会根据角色返回特定的数据; 5. 拥有基础数据管理模块,管理管理模块,角色管理模块,权限管理模块,客户管理模块,订单管理模块和业务处理模块; 6. 能够实现完整的业务流程:添加客户、添加订单、订单处理、财务报表。 ## 细节介绍 1. 业务员访问时,只能访问客户管理、订单管理、业务处理菜单,且只有添加的权力,没有修改和删除的权力; 2. 业务员添加订单时,只能基于自己拥有的客户添加; 3. 业务经理拥有客户管理、订单管理、业务处理的所有功能,可以查看所有业务员的客户和订单; 4. 总经理角色可以查看所有数据,但是没有修改的权力; 5. 仓管员只有业务处理中,测量物品信息和入库的功能,其他数据不可修改; 6. 财务可以查看审核订单价格详情并导出为 excel 表格。 -------- <项目介绍> 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值