oauth2.0---heima
程序员文章站
2022-06-13 15:46:34
...
视频:https://www.bilibili.com/video/BV1VE411h7aL?from=search&seid=12021287353823413085
代码:
---
权限模型至少是5张表:
---
代码:
登录:security-springmvc
http://localhost:8080/security-springmvc/r/r1
http://localhost:8080/security-springmvc
---
代码:
有了springSecurity之后拦截器就不要了。
UserDetailService。
核心的配置类:
package com.itheima.security.springmvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/**
* @author Administrator
* @version 1.0
**/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//定义用户信息服务(查询用户信息)
@Bean
public UserDetailsService userDetailsService(){
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("zhangsan").password("123").authorities("p1").build());
manager.createUser(User.withUsername("lisi").password("456").authorities("p2").build());
return manager;
}
//密码编码器
@Bean
public PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
//安全拦截机制(最重要)
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/r/r1").hasAuthority("p1")
.antMatchers("/r/r2").hasAuthority("p2")
.antMatchers("/r/**").authenticated()//所有/r/**的请求必须认证通过
.anyRequest().permitAll()//除了/r/**,其它的请求可以访问
.and()
.formLogin()//允许表单登录
.successForwardUrl("/login-success");//自定义登录成功的页面地址
}
}
---14---
跟踪下代码看下源码:
进入到doFilter方法
---15---
UserDetailService
自定义UserDetailService,
---16---
password的编码设置:
测试下加密:
---17---
决策的实现:
---18---
创建数据库:
---20---
会话管理:
访问:
---21---
老的授权模式基于路径授权的。
不建议基于角色授权。
新的授权模式:
---24---
推荐阅读