文章目录
关于权限
- 关于权限
- BCrypt 密码加密
做任何安全性的工作,要先认证,再授权.
BCrypt 密码加密任何应用考虑到安全,绝不能明文的方式保存密码。密码应该通过哈希算法进行加密。 有很多标准的算法比如SHA或者MD5,结合salt(盐)是一个不错的选择。 Spring Security 提供了BCryptPasswordEncoder类,实现Spring的PasswordEncoder接口使用BCrypt强 哈希方法来加密密码。 BCrypt强哈希方法 每次加密的结果都不一样。
在tensquare_user中引入依赖
org.springframework.boot
spring-boot-starter-security
spring-security默认的配置是拦截所有的请求和资源. 在如下的路径下,写配置类 com.tensquare.user.config.WebSecurityConfig 使用@Configuration注解,代表这是一个配置类 同时继承WebSecurityConfigurerAdapter 才能在启动时加载
package com.tensquare.user.config;
import org.springframework.context.annotation.Configuration;
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;
/**
* 安全配置类 @Configuration 表明了注解的配置类
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
/**
* 方法名: configure
* 方法描述:
* authorizeRequests 表示所有Security 全注解配置实现的开端, 表示开始说明需要的权限,
* 需要的权限分为2部分
* 第一部分为拦截的路径, antMatchers("/**")代表拦截所有的路径, permitAll()代表的是放行所有的
* 第二部分为访问该路径需要的权限, anyRequest() 表示任何的请求, authenticated() 表示认证后才能访问
*
* csrf().disable() 取消跨域的拦截, 降低Security级别
* 修改日期: 2019/1/13 19:36
* @param http
* @return void
* @author taohongchao
* @throws
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}