SptringSecurity 学习笔记(一)Security模式
第一种 :basic认证(早期) 过时了
是一种简单的HTTP认证
简单的说 就是访问接口时需要输入账号密码进行授权
添加依赖
<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-freemarker</artifactId>
</dependency>
配置类
package com.lfy.study.config;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.password.NoOpPasswordEncoder;
import org.springframework.stereotype.Component;
@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 新增Security
*
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//新增账户 可以访问什么接口
auth.inMemoryAuthentication()
.withUser("admin")
.password("12345")
.authorities("/");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//配置认证方式 1.token 2.form
//拦截所有请求 并走httpbasic模式
http.authorizeRequests()
.antMatchers("/**")
.fullyAuthenticated().and().httpBasic();
}
/**
* 解决 there is no passwordEncoder mapper for thi id "null"
* 原因 :升级Security5.0以上密码支持多种加密方式 这个方法可以恢复以前模式 还有一种方法就是创建Sercurity时候添加加密方式
*
* @return
*/
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
}
原理: 验证账号密码是否相同 如果相同则返回一个cookie给浏览器 如果清除缓存则需要重新输入账号密码
第二种 form 表单模式
- 将拦截模式变成了formLogin();
- 可通过 .antMatchers(“/**”).hasAnyAuthority(“filterName”)配置 拦截规则 filterName拦截规则名称
@Override
protected void configure(HttpSecurity http) throws Exception {
//配置认证方式 1.token 2.form
//拦截所有请求 并走form模式
http.authorizeRequests()
.antMatchers("/**").hasAnyAuthority("filterName").and().formLogin();
}
3.添加账户 配置加上 账户授权可以访问哪些接口
/**
* 新增Security
*
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//新增账户 可以访问什么接口
auth.inMemoryAuthentication()
.withUser("admin")
.password("12345")
.authorities("filterName");//fiterName 是接口的
}
完整代码
package com.lfy.study.config;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.password.NoOpPasswordEncoder;
import org.springframework.stereotype.Component;
@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 新增Security
*
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//配置拦截规则
//新增账户 可以访问什么接口
auth.inMemoryAuthentication()
.withUser("admin")
.password("12345")
.authorities("filterName");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//配置认证方式 1.token 2.form
//拦截所有请求 并走httpbasic模式
http.authorizeRequests()
.antMatchers("/**").hasAnyAuthority("admin").and().formLogin();
}
/**
* 解决 there is no passwordEncoder mapper for thi id "null"
* 原因 :升级Security5.0以上密码支持多种加密方式 这个方法可以恢复以前模式 还有一种方法就是创建Sercurity时候添加加密方式
*
* @return
*/
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
}
重点是将这种形式转换为动态形式才行
另
.permitAll()配置不需要校验权限
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").hasAnyAuthority("admin")
.antMatchers("/login").permitAll()//配置login接口不需要校验权限
.and().formLogin()
.loginPage("/login").and().csrf().disable()//用自己的登录页面 不用默认的登录页面;
}