开发者学堂课程【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、更多详细规定
登录页面
//定义认证规则
@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");|
重启
登录普通武功秘籍
普通武功秘籍可以访问