Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。它是一个轻量级的安全框架,它确保基于Spring的应用程序提供身份验证和授权支持。它与Spring MVC有很好地集成,并配备了流行的安全算法实现捆绑在一起。安全主要包括两个操作“认证”与“验证”(有时候也会叫做权限控制)。“认证”是为用户建立一个其声明的角色的过程,这个角色可以一个用户、一个设备或者一个系统。“验证”指的是一个用户在你的应用中能够执行某个操作。在到达授权判断之前,角色已经在身份认证过程中建立了。
个人对Spring Security的执行过程大致理解(仅供参考)
Spring Security的核心配置类是 WebSecurityConfigurerAdapter,抽象类
这是权限管理启动的入口,这里我们自定义一个实现类去它。然后编写我们需要处理的控制逻辑。
下面是代码,里面写的注释也比较详细。在里面还依赖了几个自定义的类,都是必须配置的。分别是
HrService,
MyFilterInvocationSecurityMetadataSource,
MyAccessDecisionManager,
MyAccessDeniedHandler,
MyAuthenticationFailureHandler,
MyAuthenticationSuccessHandler,
MyLogoutSuccessHandler
后面会分别解析它们
import com.galen.security.service.HrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
/**
* @Author: Galen
* @Date: 2019/3/27-14:43
* @Description: spring-security权限管理的核心配置
**/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) //全局
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private HrService hrService; //实现了UserDetailsService接口
@Autowired
private MyFilterInvocationSecurityMetadataSource filterMetadataSource; //权限过滤器(当前url所需要的访问权限)
@Autowired
private MyAccessDecisionManager myAccessDecisionManager;//权限决策器
@Autowired
private MyAccessDeniedHandler deniedHandler;//自定义错误(403)返回数据
/**
* @Author: Galen
* @Description: 配置userDetails的数据源,密码加密格式
* @Date: 2019/3/28-9:24
* @Param: [auth]
* @return: void
**/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(hrService)
.passwordEncoder(new BCryptPasswordEncoder());
}
/**
* @Author: Galen
* @Description: 配置放行的资源
* @Date: 2019/3/28-9:23
* @Param: [web]
* @return: void
**/
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/index.html", "/static/**", "/login_p", "/favicon.ico")
// 给 swagger 放行;不需要权限能访问的资源
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/images/**", "/webjars/**", "/v2/api-docs", "/configuration/ui", "/configuration/security");
}
/**
* @Author: Galen
* @Description: HttpSecurity包含了原数据(主要是url)
* 通过withObjectPostProcessor将MyFilterInvocationSecurityMetadataSource和MyAccessDecisionManager注入进来
* 此url先被MyFilterInvocationSecurityMetadataSource处理,然后 丢给 MyAccessDecisionManager处理
* 如果不匹配,返回 MyAccessDeniedHandler
* @Date: 2019/3/27-17:41
* @Param: [http]
* @return: void
**/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
@Override
public <O extends FilterSecurityInterceptor> O postProcess(O o) {
o.setSecurityMetadataSource(filterMetadataSource);
o.setAccessDecisionManager(myAccessDecisionManager);
return o;
}
})
.and()
.formLogin().loginPage("/login_p").loginProcessingUrl("/login")
.usernameParameter("username").passwordParameter("password")
.failureHandler(new MyAuthenticationFailureHandler())
.successHandler(new MyAuthenticationSuccessHandler())
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessHandler(new MyLogoutSuccessHandler())
.permitAll()
.and().csrf().disable()
.exceptionHandling().accessDeniedHandler(deniedHandler);
}
}
1.HrService
HrService实现了UserDetailsService接口中的loadUserByUsername方法,方法执行成功后返回UserDetails对象,为构建Authentication对象提供必须的信息。UserDetails中包含了用户名,密码,角色等信息
@Service
@Transactional
public class HrService implements UserDetailsService {
@Autowired
private HrMapper hrMapper;
/**
* @Author: Galen
* @Description: 实现了UserDetailsService接口中的loadUserByUsername方法
* 执行登录,构建Authentication对象必须的信息,
* 如果用户不存在,则抛出UsernameNotFoundException异常
* @Date: 2019/3/27-16:04
* @Param: [s]
* @return: org.springframework.security.core.userdetails.UserDetails
**/
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
/**
* @Author: Galen
* @Description: 查询数据库,获取登录的用户信息
**/
Hr hr = hrMapper.loadUserByUsername(s);
if (hr == null) {
throw new UsernameNotFoundException("用户名不对");
}
return hr;
}
}
2.MyFilterInvocationSecurityMetadataSource
自定义权限过滤器,继承了 SecurityMetadataSource(权限资源接口),过滤所有请求,核查这个请求需要的访问权限;主要实现Collection<ConfigAttribute> getAttributes(Object o)
方法,此方法中可编写用户逻辑,根据用户预先设定的用户权限列表,返回访问此url需要的权限列表。
package com.galen.security.interceptor;
import com.galen.security.pojo.Menu;
import com.galen.security.model.Role;
import com.galen.security.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import java.util.Collection;
import java.util.List;
/**
* @Author: Galen
* @Date: 2019/3/27-16:54
* @Description: FilterInvocationSecurityMetadataSource(权限资源过滤器接口)继承了 SecurityMetadataSource(权限资源接口)
* Spring Security是通过SecurityMetadataSource来加载访问时所需要的具体权限;Metadata是元数据的意思。
* 自定义权限资源过滤器,实现动态的权限验证
* 它的主要责任就是当访问一个url时,返回这个url所需要的访问权限
**/
@Component
public class MyFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
@Autowired
private MenuService menuService;
private AntPathMatcher antPathMatcher = new AntPathMatcher();
private static final Logger log = LoggerFactory.getLogger(MyFilterInvocationSecurityMetadataSource.class);
/**
* @Author: Galen
* @Description: 返回本次访问需要的权限,可以有多个权限
* @Date: 2019/3/27-17:11
* @Param: [o]
* @return: java.util.Collection<org.springframework.security.access.ConfigAttribute>
**/
@Override
public Collection<ConfigAttribute> getAttributes(Object o) {
String requestUrl = ((FilterInvocation) o).getRequestUrl();
//去数据库查询资源
List<Menu> allMenu = menuService.getAllMenu();
for (Menu menu : allMenu) {
if (antPathMatcher.match(menu.getUrl(), requestUrl)
&& menu.getRoles().size() > 0) {
List<Role> roles = menu.getRoles();
int size = roles.size();
String[] values = new String[size];
for (int i = 0; i < size; i++) {
values[i] = roles.get(i).getName();
}
log.info("当前访问路径是{},这个url所需要的访问权限是{}", requestUrl, values);
return SecurityConfig.createList(values);
}
}
/**
* @Author: Galen
* @Description: 如果本方法返回null的话,意味着当前这个请求不需要任何角色就能访问
* 此处做逻辑控制,如果没有匹配上的,返回一个默认具体权限,防止漏缺资源配置
**/
log.info("当前访问路径是{},这个url所需要的访问权限是{}", requestUrl, "ROLE_LOGIN");
return SecurityConfig.createList("ROLE_LOGIN");
}
/**
* @Author: Galen
* @Description: 此处方法如果做了实现,返回了定义的权限资源列表,
* Spring Security会在启动时校验每个ConfigAttribute是否配置正确,
* 如果不需要校验,这里实现方法,方法体直接返回null即可。
* @Date: 2019/3/27-17:12
* @Param: []
* @return: java.util.Collection<org.springframework.security.access.ConfigAttribute>
**/
@Override
public Collection<ConfigAttribute> getAllConfigAttributes() {
return null;
}
/**
* @Author: Galen
* @Description: 方法返回类对象是否支持校验,
* web项目一般使用FilterInvocation来判断,或者直接返回true
* @Date: 2019/3/27-17:14
* @Param: [aClass]
* @return: boolean
**/
@Override
public boolean supports(Class<?> aClass) {
return FilterInvocation.class.isAssignableFrom(aClass);
}
}
3.MyAccessDecisionManager
自定义权限决策管理器,需要实现AccessDecisionManager 的 void decide(Authentication auth, Object object, Collection<ConfigAttribute> cas)
方法,在上面的过滤器中,我们已经得到了访问此url需要的权限;那么,decide方法,先查询此用户当前拥有的权限,然后与上面过滤器核查出来的权限列表作对比,以此判断此用户是否具有这个访问权限,决定去留!所以顾名思义为权限决策器。
package com.galen.security.interceptor;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Iterator;
/**
* @Author: Galen
* @Date: 2019/3/27-16:59
* @Description: Decision决定的意思。
* 有了权限资源(MyFilterInvocationSecurityMetadataSource),知道了当前访问的url需要的具体权限,接下来就是决策当前的访问是否能通过权限验证了
* MyAccessDecisionManager 自定义权限决策管理器
**/
@Component
public class MyAccessDecisionManager implements AccessDecisionManager {
/**
* @Author: Galen
* @Description: 取当前用户的权限与这次请求的这个url需要的权限作对比,决定是否放行
* auth 包含了当前的用户信息,包括拥有的权限,即之前UserDetailsService登录时候存储的用户对象
* object 就是FilterInvocation对象,可以得到request等web资源。
* configAttributes 是本次访问需要的权限。即上一步的 MyFilterInvocationSecurityMetadataSource 中查询核对得到的权限列表
* @Date: 2019/3/27-17:18
* @Param: [auth, object, cas]
* @return: void
**/
@Override
public void decide(Authentication auth, Object object, Collection<ConfigAttribute> cas) {
Iterator<ConfigAttribute> iterator = cas.iterator();
while (iterator.hasNext()) {
if (auth == null) {
throw new AccessDeniedException("当前访问没有权限");
}
ConfigAttribute ca = iterator.next();
//当前请求需要的权限
String needRole = ca.getAttribute();
if ("ROLE_LOGIN".equals(needRole)) {
if (auth instanceof AnonymousAuthenticationToken) {
throw new BadCredentialsException("未登录");
} else
return;
}
//当前用户所具有的权限
Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
for (GrantedAuthority authority : authorities) {
if (authority.getAuthority().equals(needRole)) {
return;
}
}
}
throw new AccessDeniedException("权限不足!");
}
@Override
public boolean supports(ConfigAttribute configAttribute) {
return true;
}
@Override
public boolean supports(Class<?> aClass) {
return true;
}
}
4.MyAccessDeniedHandler;MyAuthenticationFailureHandler;MyAuthenticationSuccessHandler;MyLogoutSuccessHandler
之所以一起描述这几个类,因为这几个都是处理器。根据类名也很容易看得出,分别是拒签(403响应)处理器,认证失败处理器,认证成功处理器,注销成功处理器。通过上面的用户认证接口(UserDetails),过滤器,决策器;我们已经成功处理了权限的认证,决定当前用户的去留,然后不同的逻辑启用不同的处理器。
/**
* @Author: Galen
* @Date: 2019/3/27-17:36
* @Description: Denied是拒签的意思
* 此处我们可以自定义403响应的内容,让他返回我们的错误逻辑提示
**/
public class MyAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse resp,
AccessDeniedException e) throws IOException {
resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
resp.setContentType("application/json;charset=UTF-8");
PrintWriter out = resp.getWriter();
RespBean error = RespBean.error("权限不足,请联系管理员!");
out.write(new ObjectMapper().writeValueAsString(error));
out.flush();
out.close();
}
}
/*****************************************************************************************/
/**
* @Author: Galen
* @Date: 2019/3/28-9:17
* @Description: 认证失败的处理
**/
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
response.setContentType("application/json;charset=utf-8");
RespBean respBean;
if (exception instanceof BadCredentialsException ||
exception instanceof UsernameNotFoundException) {
respBean = RespBean.error("账户名或者密码输入错误!");
} else if (exception instanceof LockedException) {
respBean = RespBean.error("账户被锁定,请联系管理员!");
} else if (exception instanceof CredentialsExpiredException) {
respBean = RespBean.error("密码过期,请联系管理员!");
} else if (exception instanceof AccountExpiredException) {
respBean = RespBean.error("账户过期,请联系管理员!");
} else if (exception instanceof DisabledException) {
respBean = RespBean.error("账户被禁用,请联系管理员!");
} else {
respBean = RespBean.error("登录失败!");
}
response.setStatus(401);
new GalenWebMvcWrite().writeToWeb(response, respBean);
}
}
/*****************************************************************************************/
/**
* @Author: Galen
* @Date: 2019/3/28-9:17
* @Description: 认证成功的处理
**/
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.setContentType("application/json;charset=utf-8");
RespBean respBean = RespBean.ok("登录成功!", HrUtils.getCurrentHr());
new GalenWebMvcWrite().writeToWeb(response, respBean);
System.out.println("登录成功!");
}
}
/*****************************************************************************************/
/**
* @Author: Galen
* @Date: 2019/3/28-9:21
* @Description: 注销登录处理
**/
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.setContentType("application/json;charset=utf-8");
RespBean respBean = RespBean.ok("注销成功!");
new GalenWebMvcWrite().writeToWeb(response, respBean);
System.out.println("注销成功!");
}
}
5.项目结构一览
6.分布式系统中,采用redis做共享会话,解决共享会话的问题
spring security将sessionId放在header里,用户登陆后,如何通过sessionId保证已经登陆呢 解决办法如下:
@Configuration
//maxInactiveIntervalInSeconds session超时时间,单位秒
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)
public class RedisSessionConfig {
//这里有个小坑,如果服务器用的是云服务器,不加这个会报错
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
//session策略,这里配置的是Header方式(有提供Header,Cookie等方式)
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
}
}
从代码中,关键是HeaderHttpSessionStrategy,该代码定义了如果sessionId存在header里,且key为x-auth-token,就能保证调用的正确性