[SpringSecurity]web权限方案_用户认证_查询数据库完成认证

#mysql 数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
spring.datasource.username=root 
spring.datasource.password=root

package com.atguigu.securitydemo1.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;@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {@Autowiredprivate UserDetailsService userDetailsService;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsService).passwordEncoder(password());}@BeanPasswordEncoder password(){return new BCryptPasswordEncoder();}
}

第一步 引入依赖

整合MyBatisPlus完成数据库操作

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--lombok 用来简化实体类--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
</dependencies>

第二步 创建数据库和数据库表

在这里插入图片描述

第三步 创建users表对应实体类

package com.atguigu.securitydemo1.entity;import lombok.Data;@Data
public class Users {private Integer id;private String username;private String password;}

第四步 整合mp,创建接口,继承mp的接口

package com.atguigu.securitydemo1.entity;import lombok.Data;@Data
public class Users {private Integer id;private String username;private String password;}

第五步 在MyUserDetailsService调用mapper里面的方法查询数据库进行用户认证

package com.atguigu.securitydemo1.service;import com.atguigu.securitydemo1.entity.Users;
import com.atguigu.securitydemo1.mapper.UsersMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {@Autowiredprivate UsersMapper usersMapper;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//调用userMapper方法,根据用户名查询数据库QueryWrapper<Users> wrapper = new QueryWrapper<>();//where username = ?wrapper.eq("username",username);Users users = usersMapper.selectOne(wrapper);//判断if (users==null)//数据库没有用户名,认证失败{throw new UsernameNotFoundException("用户名不存在!");}List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("rold");return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),auths);}
}

第六步 在启动类添加注解MapperScan

package com.atguigu.securitydemo1;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.atguigu.securitydemo1.mapper")
public class Securitydemo1Application {public static void main(String[] args) {SpringApplication.run(Securitydemo1Application.class, args);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/308754.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.Net Core 2.2升级3.1的避坑指南

写在前面微软在更新.Net Core版本的时候&#xff0c;动作往往很大&#xff0c;使得每次更新版本的时候都得小心翼翼&#xff0c;坑实在是太多。往往是悄咪咪的移除了某项功能或者组件&#xff0c;或者不在支持XX方法&#xff0c;这就很花时间去找回需要的东西了&#xff0c;下面…

[SpringSecurity]web权限方案_用户认证_自定义用户登录页面

在配置类中实现相关的配置 Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() //自定义自己编写的登陆页面.loginPage("/login.html") //登陆页面设置.loginProcessingUrl("/user/login") //登陆访问路径.defa…

Asp.Net Core Blazor之容器部署

写在前面Docker作为开源的应用容器引擎&#xff0c;可以让我们很轻松的构建一个轻量级、易移植的容器&#xff0c;通过Docker方式进行持续交付、测试和部署&#xff0c;都是极为方便的&#xff0c;并且对于我们开发来说&#xff0c;最直观的优点还是解决了日常开发中的环境配置…

[SpringSecurity]web权限方案_用户授权_基于权限访问控制_基于角色访问控制_hasAuthority和hasAnyAuthority_hasRole和hasAnyRole

基于角色或权限进行访问控制 hasAuthority 方法 如果当前的主体具有指定的权限&#xff0c;则返回 true,否则返回 false 在配置类设置当前访问地址有哪些 Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() //自定义自己编写的登…

.Net Core WebAPI + Axios +Vue 实现下载与下载进度条

写在前面老板说&#xff1a;系统很慢&#xff0c;下载半个小时无法下载&#xff0c;是否考虑先压缩再给用户下载&#xff1f;本来是已经压缩过了&#xff0c;不过第一反应应该是用户下的数量多&#xff0c;导致压缩包很大&#xff0c;然后自己测试发现&#xff0c;只是等待的时…

[SpringSecurity]web权限方案_用户授权_自定义403页面

自定义403页面 unauth.html <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title> </head> <body><h1>没有访问权限</h1></body> </html>配置类…

三分钟Docker-环境搭建篇

如题目显示&#xff0c;三分钟让你学会在windows上安装docker环境&#xff0c;开启docker之旅的第一步。安装前要求Windows 10 64位&#xff1a;专业版&#xff0c;企业版或教育版&#xff08;内部版本16299或更高版本&#xff09;。必须启用Hyper-V控制面板->程序和功能-&g…

[SpringSecurity]web权限方案_用户注销

用户注销 在配置类中添加退出映射地址 //退出http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/hello").permitAll();测试 修改配置类&#xff0c;登陆成功之后跳转到成功页面 在成功页面添加超链接&#xff0c;写设置退出路径 success.htm…

骚年快答 | 为何微服务项目都使用单体代码仓库?

【答疑解惑】| 作者 / Edison Zhou这是恰童鞋骚年的第265篇原创内容之前在学习微软的示例eShopOnContainers时发现它使用的是单体代码仓库库&#xff0c;之后又发现大家在进行微服务项目开发时也都在使用单体代码仓库。问题来了&#xff0c;为啥要微服务项目都要使用单体仓库&a…

[SpringSecurity]web权限方案_自动登陆_原理分析和具体实现

自动登陆 1.cookie技术 2.安全框架机制实现自动登陆 这里我们使用安全框架机制实现自动登陆技术 实现原理 具体实现 第一步 创建数据库 CREATE TABLE persistent_logins (username varchar(64) NOT NULL,series varchar(64) NOT NULL,token varchar(64) NOT NULL,last_us…

[SpringSecurity]web权限方案_CSRF功能

CSRF CSRF功能默认是已经打开了&#xff01; 具体过程可以阅读CsrfFilter这个过滤器的源码 CSRF 理解 在登录页面添加一个隐藏域 <input type"hidden"th:if"${_csrf}!null"th:value"${_csrf.token}"name"_csrf "/>关闭安全…

[SpringSecurity]web权限方案_用户授权_注解使用

注解使用 Secured 判断用户是否具有角色&#xff0c;可以访问方法&#xff0c;另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。 使用注解先要开启注解功能&#xff01; 启动类(配置类)开启注解 EnableGlobalMethodSecurity(securedEnable true) 在controller的…

我和ABP vNext 的故事

Abp VNext是Abp的.NET Core 版本&#xff0c;但它不仅仅只是代码重写了。Abp团队在过去多年社区和商业版本的反馈上做了很多的改进。包括性能、底层的框架设计&#xff0c;它融合了更多优雅的设计实践。不管你是自己需要快速上手项目、或者是公司的研发团队没有足够的能力去完整…

微软为 Visual Studio 推出新的 Razor 编辑器

随着 Visual Studio 最新版本的发布&#xff0c;微软推出了一款新的 Razor 编辑器&#xff0c;用于使用 MVC、Razor Pages 和 Blazor 进行本地开发。该工具目前还处于实验状态。Razor 是一种基于 HTML 和 C# 的模板语言&#xff0c;可以用来为 .NET Web 应用程序创建动态内容。…

禁用了云服务器的网卡怎么办?

点击上方关注“汪宇杰博客” ^_^导语我们平时管理云服务器时&#xff0c;难免误操作把网卡给禁用了&#xff0c;于是再也无法远程连接了。这时候怎么办呢&#xff1f;如果有虚拟机快照&#xff0c;能够恢复到上一个良好的时刻&#xff0c;但通常会损失这个时间段内的数据和应用…

[SpringBoot2]@MatrixVariableUrlPathHelper

场景 页面开发&#xff0c;cookie禁用了&#xff0c;session里面的内容怎么使用&#xff1a; session.set(a,b)—>jessionid—>cookie—>每次发请求携带 此时cookie禁用了&#xff0c;我们要怎么得到session里面的内容呢&#xff1f; url重写&#xff1a;/abc;jse…

WebBenchmark之动态数据测试

对于很多WebApi管理工具来说&#xff0c;针对接口的性能测试都拿固定的数据进行一个循环式的测试&#xff1b;这种测试只能确保当前数据下的有效性&#xff0c;但在性能测试中往往需要压测不同的数据分布&#xff0c;这样能够更准确地反映在不同数据下系统的处理能力。WebBench…

[SpringBoot2]Thymeleaf

引入starter <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>自动配置好了thymeleaf Configuration(proxyBeanMethods false) EnableConfigurationPrope…

【Ids4实战】分模块保护资源API

(毕竟西湖六月中)书接上文&#xff0c;上回书咱们说到了IdentityServer4&#xff08;下文统称Ids4&#xff09;官方已经从v3更新升级到了v4版本&#xff0c;我的Blog.Idp项目也做了同步更新&#xff0c;主要是针对快速启动UI做的对应修改&#xff0c;毕竟Ids4类库nuget包更新就…

[SpringBoot2]拦截器

拦截器 1.编写一个拦截器实现HandlerInterceptor接口2.拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)3.指定拦截规则[如果是拦截所有&#xff0c;静态资源也会被拦截] 1、HandlerInterceptor 接口 /*** 登录检查* 1、配置好拦截器要拦截哪些请求* 2、把这些配…