多个HttpSecurity
如果业务比较复杂,则需要配置多个HttpSecurity,
对WebSecurityConfigurerAdapter 进行多次扩展。
配置多个 HTTPpSecurity时,MultiHttpSecurityConfig 不需要继承 WebSecurityConfigurerAdapter,
在 MultiHttpSecurityConfig 中创建静态内部类继承
WebSecurityConfigurerAdapter 即可,
静态内部类 添加@Configuration 注解和@Order注解,
@Order 注解表示该配置的优先级,数字小优先级越大,
未配置@Order 解的配置优先级最小
加密
springsecurity默认使用BCryptPasswordEncoder加密方法
BCryptPasswordEncoder使用 BCrypt 强哈希函数,
开发者在使用时可以选择提供由strength和 SecureRandom 实例, strength 越大,密钥的迭代次数越多,密钥迭代次数为2的strength次方,strength 取值在4–31 之间,默认为 10
SecureRandom生成随机数,采用的是类似于密码学的随机数生成规则,其输出结果较难预测
用户注册的时候,也要进行加密,代码类似于
@Service
public class RegService {
public int reg(String username, String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
String encodePasswod = encoder.encode(password);
return saveToDb(username, encodePasswod);
}
}
方法安全
上文介绍的认证与授权都是基于 URL 的,开发者也可以通过注解来灵活地配置方法安全,要使用相关注解,
首先要通过@EnableGloba!MethodSecurity 注解开启基于注解的安全配置:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class MultiHttpSecurityConfig {
}
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
prePostEnabled=true会解锁**@PreAuthorize和 @PostAuthorize
@PreAuthorize注解会在方法执行前进行验证,
而@PostAuthorize注解会在方法执行后**进行验证。
securedEnabled=true 会解锁@Secured 注解。
@Service
public class MethodService {
//@Secured(”ROLE_ AD MIN")注解表示访问该方法需要 ADMIN 角色,注意这里需要在角色前加一个前缀ROLE_
@Secured("ROLE_ADMIN")
public String admin() {
return "hello admin";
}
//@PreAuthorize("hasRole('ADMIN') and hasRole('DBA')")注解表示访问该方法既需要 ADMIN角色又需要 DBA 角色
@PreAuthorize("hasRole('ADMIN') and hasRole('DBA')")
public String dba() {
return "hello dba";
}
//同理
@PreAuthorize("hasAnyRole('ADMIN','DBA','USER')")
public String user() {
return "user";
}
}
总配置文件
package com.zicheng.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class MultiHttpSecurityConfig {
/*
BCryptPasswordEncoder 使用 BCrypt 强哈希函数,
开发者在使用时可以选择提供由 strength 和 SecureRandom 实例, strength 越大,密钥的迭代次数越多,密钥 是代次数为2的strength次方,
strength 取值在4--31 之间,默认为 10
SecureRandom生成真随机数,采用的是类似于密码学的随机数生成规则,其输出结果较难预测
*/
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(10);
}
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//密码,同样是123。由于迭代次数和真随机数的不同,加密后的密码也是不一样
auth.inMemoryAuthentication()
.withUser("root").password("$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq")
.roles("ADMIN", "DBA")
.and()
.withUser("admin").password("$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq")
.roles("ADMIN", "USER")
.and()
.withUser("sang")
.password("$2a$10$eUHbAOMq4bpxTvOVz33LIehLe3fu6NwqC9tdOcxJXEhyZ4simqXTC")
.roles("USER");
}
/**
* 配置多个 HTTPpSecurity时,MultiHttpSecurityConfig 不需要继承 WebSecurityConfigurerAdapter,
* 在 MultiHttpSecurityConfig 中创建静态内部类继承 WebSecurityConfigurerAdapter 即可,
* 静态内部类 添加@Configuration 注解和@Order 注解,
*
* @Order 注解表示该配置的优先级,数字小优先级越大,
* 未配置@Order 解的配置优先级最小
*/
//表示该类主要用来处理“/admin/**”模式的 URL,其他则由另一个 HttpSecurity 来处理
@Configuration
@Order(1)
public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/admin/**").authorizeRequests()
.anyRequest().hasRole("ADMIN");
}
}
@Configuration
public static class OtherSecurityConfig
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginProcessingUrl("/login")
.permitAll()
.and()
.csrf()
.disable();
}
}
}
.