SpringBoot整合SpringSecurity(附源码)

java1234

共 11709字,需浏览 24分钟

 · 2020-10-24

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

66套java从入门到精通实战课程分享

在前几篇博客里,我们对于SpringBoot框架的项目中的认证还是采用最朴素的拦截器来实现的,那SpringBoot这么高级,就没有什么成熟的解决方案吗?有的,Spring Security,今天我们就来认识Spring Security,再配上一个demo加深理解。

Spring Security简介

Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理。

记住常用的几个类:

  • WebSecurityConfigurerAdapter:自定义 Security 策略

  • AuthenticationManagerBuilder:自定义认证策略

  • @EnableWebSecurity:开启 WebSecurity 模式

Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。

“认证”(Authentication)

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

“授权” (Authorization)

授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

那实际上除了SpringSecurity,用的比较多的安全框架还有shiro。可以下宏观的了解一下

SpringSecurity和Shiro的相同点和不同点。

  • 相同点

认证功能、授权功能、加密功能、会话管理、缓存支持、rememberMe功能

  • 不同点

1、SpringSecurity基于Spring开发,项目中如果使用Spring作为基础,配合SpringSecurity做权限更加方便,而Shiro需要和Spring进行整合开发

2、SpringSecurity功能比Shiro更加丰富些,例如安全防护

3、SpringSecurity社区资源比Shiro丰富

4、Shiro配置和使用比较简单,SpringSecurity上手复杂

5、Shiro依赖性低,不需要任何框架和容器,可以独立运行,而SpringSecurity依赖于Spring容器。

测试Demo

前置准备

首先创建一个SpringBoot项目,勾选SpringSecurity模块

或者创建项目后导入依赖


   org.springframework.boot
   spring-boot-starter-security


为了方便前端展示,我们还导入thymeleaf依赖

创建几个前端页面(用于后面来测试权限访问)提取码:o9dz

项目结构如下图所示

测试主页及跳转(测试时先把SpringSecurity依赖注释掉

编写基础配置类

在项目下创建config包,新建SecurityConfig.java

package com.feng.config;

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;

/**
 * 

springsecurity-test


 * 


 *
 * @author : Nicer_feng
 * @date : 2020-10-13 11:38
 **/
@EnableWebSecurity  //开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {

     // 授权规则
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 首页所有人可以访问
        // 其他界面只有对应的角色(权限)才可以访问
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
    }
}

测试

开启后我们再测试下

可以发现报了403forbidden错误

There was an unexpected error (type=Forbidden, status=403).
Access Denied

我们在配置类中添加如未登录强制跳转到login页面

这里的http.formLogin();表示开启自动配置的登录功能,如果无权限则跳转到/login

测试发现确实如此,如果没有权限则强制跳转到登录页面

但需要注意的是,这个登录页面并不是我们自己写的login页面,而是SpringSecurity自带的默认登录页面

重写认证规则

我们可以自定义认证规则,重写configure(AuthenticationManagerBuilder auth)方法来配置认证的规则。

可以看到这里的认证规则常用的有这几种,这里先用inMemoryAuthentication(内存数据库)的来演示

添加配置代码

package com.feng.config;

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;

/**
 * 

springsecurity-test


 * 


 *
 * @author : Nicer_feng
 * @date : 2020-10-13 11:38
 **/
@EnableWebSecurity  //开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("feng").password("111111").roles("vip1")
                .and()
                .withUser("user").password("22222").roles("vip2")
                .and()
                .withUser("admin").password("000000")
                .roles("vip1","vip2","vip3");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        http.formLogin();
    }
}

我们添加了三个用户,分别拥有不同的权限,重启tomcat后测试

可以发现这里报了无id映射的错误,之所以报这个错是因为从前端传过来的密码需要进行加密,否则无法登陆,我们是用官方推荐的bcrypt加密方式

    @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("feng").password(new BCryptPasswordEncoder().encode("111111"))
                    .roles("vip1")
                    .and()
                    .withUser("user").password(new BCryptPasswordEncoder().encode("222222"))
                    .roles("vip2")
                    .and()
                    .withUser("admin").password(new BCryptPasswordEncoder().encode("000000"))
                    .roles("vip1","vip2","vip3");
        }

重启测试

权限注销

在配置类中加入注销功能

@Override
    protected void configure(HttpSecurity http) throws Exception {
        ......

        //开启自动配置的注销的功能
        // /logout 注销请求
        http.logout();
    }

在index页面添加logout注销功能


"right menu">
    
    "item" th:href="@{/login}">
        "address card icon"> 登录
    
    
    "item" th:href="@{/logout}">
        "address card icon"> 注销
    


注意这里的默认提示中的/login和/logout都是SpringSecurity自带的默认界面

点击注销后,会返回登录界面

如果想要注销后仍然回到首页,可以在logout()后添加logoutSuccessUrl

http.logout().logoutSuccessUrl("/");

根据权限显示不同页面

上面我们设置了三个用户,拥有不同权限,在实际业务中,那能不能让拥有相应权限的用户只显示相应的界面呢?是可以做到的,我们需要利用thymeleaf 和SpringSecurity结合的功能

首先添加对应依赖


   org.thymeleaf.extras
   thymeleaf-extras-springsecurity5
   3.0.4.RELEASE


修改前端的页面

头部命名空间改为

"en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

网页部分

"right menu">
    
    "!isAuthenticated()">
        "item" th:href="@{/login}">
            "address card icon"> 登录
        
    


    
    "isAuthenticated()">
        "item">
            "address card icon">
            用户名:"principal.username">
            角色:"principal.authorities">
        
    


    "isAuthenticated()">
        "item" th:href="@{/logout}">
            "address card icon"> 注销
        
    



然后我们需要给相应模块附上相应的权限等级

"column" sec:authorize="hasRole('vip1')">
   "ui raised segment">
       "ui">
           "content">
               "content">Level 1
               

               
"@{/level1/1}">"bullhorn icon"> Level-1-1

               
"@{/level1/2}">"bullhorn icon"> Level-1-2

               
"@{/level1/3}">"bullhorn icon"> Level-1-3

           
       
   


"column" sec:authorize="hasRole('vip2')">
   "ui raised segment">
       "ui">
           "content">
               "content">Level 2
               

               
"@{/level2/1}">"bullhorn icon"> Level-2-1

               
"@{/level2/2}">"bullhorn icon"> Level-2-2

               
"@{/level2/3}">"bullhorn icon"> Level-2-3

           
       
   


"column" sec:authorize="hasRole('vip3')">
   "ui raised segment">
       "ui">
           "content">
               "content">Level 3
               

               
"@{/level3/1}">"bullhorn icon"> Level-3-1

               
"@{/level3/2}">"bullhorn icon"> Level-3-2

               
"@{/level3/3}">"bullhorn icon"> Level-3-3

           
       
   


测试

再次启动测试

可以看到我们做到“千人千面”了

rememberMe

在实际登录中我们肯定有记住密码这个状态,那在SpringSecurity中如何配置?十分简单,只需要在授权规则内加一行http.rememberMe()即可

重启测试下发现登录页面多了一个Remember me的选项

打开浏览器的开发者工具发现该cookies信息保存14天

并且如果我们点击注销,该cookies信息自动删除

定制登录页

实际业务中显然不可能使用SpringSecurity自带的登录界面,我们需要定制自己的登录页面,首先我们要在配置内的登录页配置后添加指定的loginpage

前端也需要指向我们自定义的登录请求

   "item" th:href="@{/toLogin}">
                "address card icon"> 登录
   

在login.html配置提交请求需要改成post

"@{/login}" method="post">
   "field">
       
       "ui left icon input">
           type="text" placeholder="Username" name="username">
           "user icon">
       
   
   "field">
       
       "ui left icon input">
           type="password" name="password">
           "lock icon">
       
   
   type="submit" class="ui blue submit button"/>


并且login.html增加记住我的选项框

type="checkbox" name="remember"> 记住我

加入这个功能时,配置类需要加入

http.rememberMe().rememberMeParameter("remember");

运行测试

发现登录都没问题,但是注销的时候缺出现了404错误,别紧张,是因为它默认防止 csrf 跨站请求伪造,因为会产生安全问题,我们可以将请求改为 post 表单提交,或者在 Spring security 中关闭 csrf 功能。在授权配置中增加 http.csrf().disable() 即可

再次运行即可

彩蛋

如果我们想用jdbc连数据库来看认证用户呢(用户信息存在数据库中)

首先在数据库中创建一个用户信息数据库,这里直接贴sql

/*
SQLyog Ultimate v12.09 (64 bit)
MySQL - 5.7.23 : Database - testspringsecurity
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`testspringsecurity` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `testspringsecurity`;

/*Table structure for table `authorities` */

DROP TABLE IF EXISTS `authorities`;

CREATE TABLE `authorities` (
  `username` varchar(50) NOT NULL,
  `authority` varchar(50) NOT NULL,
  UNIQUE KEY `ix_auth_username` (`username`,`authority`),
  CONSTRAINT `fk_authorities_users` FOREIGN KEY (`username`) REFERENCES `users` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `authorities` */

insert  into `authorities`(`username`,`authority`) values ('feng','ROLE_vip1');

/*Table structure for table `users` */

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
  `username` varchar(50) NOT NULL,
  `password` varchar(500) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `users` */

insert  into `users`(`username`,`password`,`enabled`) values ('feng','$2a$10$cxCFUH0.O.pWGnp9KhC0He2T9jmQx4AV3mcjWMvmqM6eSq/cHfxFG',1);

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


这里插入了一条数据肯定会好奇吧,下面在解释

在刚才的项目中添加mysql、jdbc依赖


        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
        
            mysql
            mysql-connector-java
            runtime
        


配置类添加链接数据库信息

application.properties


spring.thymeleaf.cache=false

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testspringsecurity?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=******


我们在配置类中注释掉inMemoryAuthentication的方法

使用jdbcAuthentication认证,改为

package com.feng.config;

import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.sql.DataSource;

/**
 * 

springsecurity-test


 * 


 *
 * @author : Nicer_feng
 * @date : 2020-10-13 11:38
 **/
@EnableWebSecurity  //开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("feng").password(new BCryptPasswordEncoder().encode("111111"))
//                .roles("vip1")
//                .and()
//                .withUser("user").password(new BCryptPasswordEncoder().encode("222222"))
//                .roles("vip2")
//                .and()
//                .withUser("admin").password(new BCryptPasswordEncoder().encode("000000"))
//                .roles("vip1","vip2","vip3");

        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("select * from users WHERE username=?")
                .authoritiesByUsernameQuery("select * from authorities where username=?")
                .passwordEncoder(new BCryptPasswordEncoder());

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        http.formLogin().loginPage("/toLogin");

        http.rememberMe().rememberMeParameter("remember");

        http.csrf().disable();

        //开启自动配置的注销的功能
        // /logout 注销请求
        http.logout().logoutSuccessUrl("/");
    }
}


重启服务器后启动项目,上面的一串密码就是123456通过BCrypt加密后符号,因为不加密不能认证

注意数据库的认证权限和用户密码信息分开存储,且用户权限前在数据库中要加入**ROLE_**前缀

123456通过BCrypt加密后为

$2a$10$1gLSaAn2i4LSiq28ACalg.jOUeTNBWfSJtt19uEySy2vcUsi7qyBy

启动后测试(并在数据库中添加账户测试登录)



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:

https://blog.csdn.net/weixin_43876186/article/details/109054037




粉丝福利:108本java从入门到大神精选电子书领取

???

?长按上方锋哥微信二维码 2 秒
备注「1234」即可获取资料以及
可以进入java1234官方微信群



感谢点赞支持下哈 

浏览 15
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报