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

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

开发者学堂课程【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

普通武功秘籍可以访问

相关文章
|
6月前
可能是由于PHPStorm的授权验证出现了问题
可能是由于PHPStorm的授权验证出现了问题
43 1
|
8月前
|
安全 前端开发 Java
基于Session方式深入了解认证授权 2
基于Session方式深入了解认证授权
29 0
|
8月前
|
Java Maven 数据安全/隐私保护
基于Session方式深入了解认证授权 1
基于Session方式深入了解认证授权
39 0
|
安全 Java 数据安全/隐私保护
权限控制 &amp; 注销|学习笔记
快速学习权限控制 &amp;注销
64 0
|
安全 Java 开发者
认证通过后显示当前认证用户名|学习笔记
快速学习认证通过后显示当前认证用户名
82 0
认证通过后显示当前认证用户名|学习笔记
|
安全 Java 数据安全/隐私保护
记住我 amp&;定制登录页|学习笔记
快速学习记住我 amp&;定制登录页
75 0
|
数据采集 存储 算法
导火索:OAuth 2.0四种授权登录方式必读
导火索:OAuth 2.0四种授权登录方式必读
116 0
导火索:OAuth 2.0四种授权登录方式必读
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL账号管理、授权、收权、过期
MySQL账号管理、授权、收权、过期
85 0
|
前端开发 JavaScript 数据库
github 授权登录教程与如何设计第三方授权登录的用户表
github 授权登录教程与如何设计第三方授权登录的用户表
673 0
github 授权登录教程与如何设计第三方授权登录的用户表
|
存储 安全 前端开发
你还不了解基于session的授权认证吗?
在漫长的开发过程中,权限认证是一个永恒不变的话题,随着技术的发展,从以前的基于sessionId的方式,变为如今的token方式。session常用于单体应用,后来由于微服务的兴起,分布式应用占了很大的一部分。本文将为大家介绍基于session的单体应用授权认证方式。后续会介绍基于token的认证方式。
225 0