春节假期已经过去一半了,该学习一下了,今天开始好好学习spring-security权限框架
1.创建springboot工程,首先看pom文件,两个核心依赖引入即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
2.访问http://localhost:8762 就会自动跳转到spring-security提供的登陆页面
3.很奇怪这个页面是怎么来的呢?用户明是什么,密码又是什么呢(告诉你控制台有spring-security生成的密码,用户名默认为user)于是我点击登陆404错误!!!为啥呢?
4看一下springboot对于spring-security的自动配置支持
此包下
org.springframework.boot.autoconfigure.security.servlet
默认登陆页面的用户密码由UserDetailsServiceAutoConfiguration 类来加载,
通过配置类
SecurityProperties来获取默认的用户配置(可以在配置文件中配置)加载进内存,密码由uuid随机生成(可看SecurityProperties类)
注意第2点,ConditionalOnMissingBean注解,如果容器中存在这些bean,
那么此类不会被加载,也就是说,如果自己配置了UserDetailsService(内存加载用户就是UserDetailsService的实现)
源码可以看出(只要导入spring-cloud-starter-security jar)就会开启对spring-security的自动配置
4.那使用user和生成的密码登陆会404呢?经过研究,是因为没有配置登陆成功的回调(定义controller,响应,所以报404,应该是这个问题。。。)
自定义登陆配置,需要继承WebSecurityConfigurerAdapter,
配置安全过滤器链,(此类中还可以配置认证管理器 ,所有 UserDetails 相关的它都管,包含 PasswordEncoder 密码机)直接上代码了,
package com.youdu.distributed.authentication.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
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.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @author Sir_小三
* @date 2020/1/28--20:34
*/
@SpringBootConfiguration
public class SecurityCongig extends WebSecurityConfigurerAdapter {
/**
* 将用户设置在内存中
*如果将这个代码放开,你自己配置了用户,那么springboot自动配置UserDetailsServiceAutoConfiguration类不会被加载,
* 自然spring-security默认的user用户也不会被加载
* @param auth
* @throws Exception
*/
// @Autowired
// public void config(AuthenticationManagerBuilder auth) throws Exception {
// // 在内存中配置用户,配置多个用户调用`and()`方法
// auth.inMemoryAuthentication()
// .passwordEncoder(passwordEncoder()) // 指定加密方式
// .withUser("admin").password(passwordEncoder().encode("123456")).roles("ADMIN")
// .and()
// .withUser("test").password(passwordEncoder().encode("123456")).roles("USER");
// }
// @Bean
// public PasswordEncoder passwordEncoder() {
// // BCryptPasswordEncoder:Spring Security 提供的加密工具,可快速实现加密加盐
// return new BCryptPasswordEncoder();
// }
/**
* 登录处理
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.cors()
.and()
.authorizeRequests().anyRequest().authenticated()//所有请求都需要进行认证
.and()
.formLogin()
.successForwardUrl("/login/success").//登陆成功以后的controller
failureForwardUrl("/login/failure");//登陆失败的controller
}
}
然后把controller的代码贴上
package com.youdu.distributed.authentication.controller;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Sir_小三
* @date 2020/1/28--23:31
*/
@RestController
@RequestMapping("/login")
public class LoginCallback {
@PostMapping("/failure")
public String loginFailure() {
return "登录失败了,老哥";
}
/**
* 登录成功后拿到个人信息.
*
* @return the rest
*/
@PostMapping("/success")
public String loginSuccess() {
// 登录成功后用户的认证信息 UserDetails会存在 安全上下文寄存器 SecurityContextHolder 中
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = principal.getUsername();
return username+"登录成功";
}
}
至此,就可以使用user和spring-security提供的密码进行登陆了。登陆成功会跳转到对应得controller
,如果不想用它默认得那就放开上面得代码,使用自己配得用户进行登陆,至于登陆页面怎么来得,后面在研究吧,该睡觉了