登录 &认证 &授权|学习笔记

简介: 快速学习登录 &认证 &授权

开发者学堂课程【SpringBoot 快速掌握 - 高级应用:登录 &认证 &授权】学习笔记,与课程紧密联系,让用户快速学习知识

课程地址https://developer.aliyun.com/learning/course/613/detail/9313


登录 &认证 &授权

security 认证与授权

使用 security 为这个文档加上认证和授权功能

package com.atguigu.security;import ...

//*

*1、引入SpringSecurity;  

*/

进入pom.xml中

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId></ dependency>

<dependency>

<groupid>org.springframework.boot</group1d>

<artifactId>spring-boot-starter-securityk / artifactId></ dependency>

1、引入 SpringSecurity;

2、编写 SpringSecurity 的配置类

搜索 spring 官网,找到 spring projects,quick start

Create New Class

Name:config.MySecurityConfigl

Kind:Class

/**

*1、引入SpringSecurity;

* 2、编写 SpringSecurity 的配置类;

*@EnableWebSecurityextendsWebSecuri tyConf igurerAdapter

* 3、控制请求的访问权限;|

实例:

publicclassSecurityConfigextendsWebSecur ityConfigurerAdapter {

@Override

protected void configure(Ht tpSecurity http) throws Exception {

http

. author izeRequests( )

. antMatchers("/css/**". "/ index"). permitAll()

. antMatchers("/user/**"). hasRole( "USER")

. formLogin()

. loginPage("/login" ). failureUrl("/login-error");

@Autowired

public void configureGlobal (Authenticat ionManagerBuilder auth) throws Exception {

auth

. inMemoryAuthentication( )

.wi thUser("user"). password( "password"). roles( "USER");

来到配置中,crtl+o打开所有可以书写的任务,

@EnableWebSecurity

c13pub1icclassMySecurityConfigextends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

//super.configure(http)

//定制请求的授权规则

http. authorizeRequests() . antMatchers( ..Patterns:. "/"). permitAll()

. antMatchers( ..ntPatterns:. "/1eve11/**"). hasRole("VIP1")

. antMatchers( ..tPatterns: "/1eve12/**") . hasRole("VIP2")

. antMatchers( .tPatterns:. "/1eve13/**"). hasRole("VIP3");|

//开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面

http.formlogin();

//1、/Login来到登陆页

//2、重定向到/login?error表示登陆失败

//3、更多详细规定

登录页面

image.png

//定义认证规则

@override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

//super.configure(auth)

auth. inMemoryAuthentication( )

.withUser( username: "zhangsan"). password("123456").roles("VIP1" , "VIP2")

.and()

.withUser( username: "lisi"). password("123456").roles("VIP2" , "VIP3")

And()

.withUser( username: "wangwu"). password("123456"). roles("VIP1" , "VIP3");|

重启

image.png

登录普通武功秘籍

image.png

普通武功秘籍可以访问

相关文章
|
12月前
|
数据采集 DataWorks 大数据
数据开发平台/工具对比测评:
数据开发平台/工具对比测评
410 23
|
存储 编译器 Linux
动态链接的魔法:Linux下动态链接库机制探讨
本文将深入探讨Linux系统中的动态链接库机制,这其中包括但不限于全局符号介入、延迟绑定以及地址无关代码等内容。
2113 141
|
9月前
|
消息中间件 运维 安全
云消息队列 ApsaraMQ Serverless 演进:高弹性低成本、更稳定更安全、智能化免运维
云消息队列 ApsaraMQ Serverless 演进:高弹性低成本、更稳定更安全、智能化免运维
233 0
|
Java 数据库连接 Nacos
Nacos2.2.3支持达梦
Nacos2.2.3支持达梦
1849 2
|
供应链 搜索推荐 API
探讨拼多多商品 API 接口:运用及收益
拼多多商品API接口为开发者和企业提供了一站式的商品数据交互解决方案,涵盖商品详情、价格、库存、评价等多元信息,助力电商平台拓展、数据分析、个性化推荐及营销推广,有效提升商业价值和用户体验,推动电商领域的创新与发展。
1106 0
这个好玩又实用的jupyter插件我真的爱了
这个好玩又实用的jupyter插件我真的爱了
166 0
|
Web App开发 监控 前端开发
Web Performance Optimization:前端性能优化全方位指南
【4月更文挑战第6天】本文是关于Web Performance Optimization的指南,重点讲述如何优化前端性能以提升用户体验和网站业务表现。关键性能指标包括First Contentful Paint (FCP)、First Meaningful Paint (FMP)、Largest Contentful Paint (LCP)、First Input Delay (FID)和Cumulative Layout Shift (CLS)。优化策略涉及资源压缩、网络配置、代码架构改进、交互渲染优化及性能监控。
2495 0
|
Kubernetes 负载均衡 网络协议
k8s中iptables与ipvs详解——2023.05
k8s中iptables与ipvs详解——2023.05
2849 0
|
资源调度 Ubuntu Unix
05-PDI(Kettle)脚本执行
文章目录 05-PDI(Kettle)脚本执行 pan和kitchen实验背景 pan命令演示
05-PDI(Kettle)脚本执行
|
编解码 人工智能 芯片
LR ps宿主免费的插件Portraiture功能介绍
Portraiture这是一款适用于PS和LR的磨皮滤镜插件,操作简便、省去了选择蒙版和逐步像素处理的繁琐流程,帮助您实现高效的肖像修饰。
592 0