一、权限管理的概念

另一个安全框架shiro:shiro之权限管理的描述


导入常用坐标

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId><version>2.3.9.RELEASE</version></dependency>

二、spring security用户认证

1.用户认证之application.properties配置文件

spring.security.user.name=admin
spring.security.user.password=admin

2.用户认证之自定义配置文件

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();String encode = passwordEncoder.encode("admin");auth.inMemoryAuthentication().withUser("admin").password(encode).roles();}@BeanPasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

3.数据库查询(这一种也是最常用的)

1.使用mybatis plus框架处理dao层

      <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency>

2.创建数据库表

3.编写mapper文件

@Repository
public interface UserMapper extends BaseMapper<Users> {}

4.编写service

package com.zsh.security.service;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsh.security.mapper.UserMapper;
import com.zsh.security.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;import java.util.List;/*** @author:抱着鱼睡觉的喵喵* @date:2021/3/12* @description:*/
@Service("userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {@Autowiredprivate UserMapper userMapper;@Overridepublic UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {QueryWrapper<Users> wrapper = new QueryWrapper<>();wrapper.eq("username", s);Users users = userMapper.selectOne(wrapper);if (users == null) {throw new UsernameNotFoundException("账号或密码错误!");} else {List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");return new User(users.getUsername(), new BCryptPasswordEncoder().encode(users.getPassword()), auths);}}
}

5.编写配置文件

package com.zsh.security.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;/*** @author:抱着鱼睡觉的喵喵* @date:2021/3/12* @description:*/
@Configuration
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {@Autowiredprivate UserDetailsService userDetailsService;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());}@BeanPasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

6.配置数据库连接

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springsecurity?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin

7.运行


过滤器链


三、自定义登录界面

1.在SecurityConfig2中加入有关http的实现方法

package com.zsh.security.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;/*** @author:抱着鱼睡觉的喵喵* @date:2021/3/12* @description:*/
@Configuration
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {@Autowiredprivate UserDetailsService userDetailsService;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin().loginPage("/login.html")   //设置登录界面.loginProcessingUrl("/user/login")  //登录界面url.defaultSuccessUrl("/test/index")     //默认登录成功界面.and().authorizeRequests()      //哪些资源可以直接访问.antMatchers("/","/test/hello","/user/loin").permitAll()    //不做处理.anyRequest().authenticated()   //所有请求都可以访问.and().csrf().disable();        //关闭CSRF}@BeanPasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}}

2.login.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body> <!--name必须是username和password --><form action="/user/login" method="post">username:<input type="text" name="username"> <br>password:<input type="password" name="password"><br><input type="submit" value="提交"></form>
</body>
</html>

3.controller

@RestController
@RequestMapping("/test")
public class SecurityController {@RequestMapping("/hello")public String hello() {return "hello! Spring Security!";}@RequestMapping("/index")public String index() {return "hello index!";}
}

4.访问http://localhost:8080/test/hello

5.访问http://localhost:8080/login.html


springboot+springsecurity+mybatis plus之用户认证相关推荐

  1. SpringBoot+SpringSecurity之多模块用户认证授权同步

    在之前的文章里介绍了SpringBoot和SpringSecurity如何继承.之后我们需要考虑另外一个问题:当前微服务化也已经是大型网站的趋势,当我们的项目采用微服务化架构时,往往会出现如下情况: ...

  2. springboot+springsecurity+mybatis plus之用户授权

    文章目录 前言 一.导入坐标 二.Users实体类及其数据库表的创建 三.controller,service,mapper层的实现 四.核心--编写配置文件 五.无权限界面和登录界面的实现 前言 即 ...

  3. springboot+springsecurity+mybatis plus注解实现对方法的权限处理

    文章目录 接上文 [springboot+springsecurity+mybatis plus之用户授权](https://blog.csdn.net/Kevinnsm/article/detail ...

  4. springboot 、thymeleaf、pagehelper 、springsecurity实现 登录,用户认证,分页的前端使用妹子UI

    springboot  .thymeleaf.pagehelper .springsecurity 实现 登录,用户认证,分页的前端使用妹子UIhttp://tpl.amazeui.org/. 项目下 ...

  5. SpringtBoot+SpringSecurity+Jwt+MyBatis整合实现用户认证以及权限控制

    文章目录 前言 数据库表结构 项目结构图 核心配置类SecurityConfig 实体类 工具类 用户登录认证 Token令牌验证 获取用户权限 用户权限验证 Service层实现类 统一响应类 Co ...

  6. springBoot+springSecurity 数据库动态管理用户、角色、权限(二)

    序:  本文使用springboot+mybatis+SpringSecurity 实现数据库动态的管理用户.角色.权限管理 本文细分角色和权限,并将用户.角色.权限和资源均采用数据库存储,并且自定义 ...

  7. springboot+springsecurity+mybatis+jwt实现单点登录(详细到爆了)

    2021年奉上我最喜欢的一句话:愿你孤独的努力都有回报,愿你前行的路上有人陪伴.加油

  8. SpringBoot+SpringSecurity+JWT实现认证和授权

    SprinBoot 系列文章: Spring Boot入门之Hello Spring Boot SpringBoot 配置多个JdbcTemplate SpringBoot 整合Mybatis CAS ...

  9. SpringBoot + SpringSecurity + Mybatis-Plus + JWT + Redis 实现分布式系统认证和授权(刷新Token和Token黑名单)

    1. 前提   本文在基于SpringBoot整合SpringSecurity实现JWT的前提中添加刷新Token以及添加Token黑名单.在浏览之前,请查看博客:   SpringBoot + Sp ...

最新文章

  1. fprintf()中的stderr解析
  2. 假如有人今天把支付宝的存储服务器炸了,支付宝里的钱是不是就没了。。。...
  3. 餐厅前台php,餐厅前台接听电话技巧
  4. 3个步骤,4大平台,完成大规模数据处理
  5. __doPostBack用法 【csdn】
  6. VLC 学习计划---文档阅读
  7. 三层架构学习的困难_“网工起航计划”3天集训营 带你了解大型企业网络架构设计!...
  8. css外观样式 1204
  9. shell批量文件编码转换
  10. 如何用iMazing软件将手机备忘录导入至电脑
  11. 智能变电站调试仿真培训系统 61850规约培训系统 免费送
  12. 锐捷文件描述错误linux,ubuntu下使用锐捷客户端连接校园网-郑州大学Ruijieclient for Linux下载及配置指导...
  13. 世界语言缩写,各国语言简称,各国域名缩写
  14. 使用Diskgenius将U盘分区,分为启动盘和文件存储两大功能详解
  15. 【py】pandas
  16. python里面and和or用法
  17. lesson 20 one man in a boat 独坐孤舟-for hours数小时做时间状语,having done于句首非谓语做状语,its wasteof time,do nothing
  18. CUPS学习五:打印机基础
  19. 人人网模拟用户登陆行为后进入包贝尔的个人主页
  20. SSH 官网下载地址

热门文章

  1. Powershell常用命令
  2. 电话开启和电话关闭的命令
  3. ssh项目放到服务器上出现404,项目运行一段时间,后台程序无法启动,404错误
  4. 插入数据到hive_Hive实现网站PV分析
  5. mac hdmi 不能调整音量_如何使用Mac的媒体键在DisplayPortHDMI或Thunderbolt监视器上调整扬声器音量...
  6. 对应chd5.14的spark_GitHub - shixiaopengql/BigData-News: 基于Spark2.2新闻网大数据实时系统项目...
  7. ubuntn终端缩小_缩小可以通过终端执行的工具
  8. 信号与系统奥本海姆第二版_【中山大学电通信通信号与系统考研】自编的两张小卡片带大家整理一下《奥本海姆·信号与系统》的知识架构...
  9. python版本升级和系统更新下载安装_Python环境安装与升级
  10. mysql aes java解密_加密/解密的Java函數,如Mysql的AES_ENCRYPT和AES_DECRYPT