一句话没有特殊要求
SecurityContextHolder:有一个ThreadLocal 来存储 SecurityContext,因此每个线程有自己独立的SecurityContext,默认策略SecurityContextHolder.MODE_THREADLOCAL,你也可以自己配成SecurityContextHolder.MODE_GLOBAL或SecurityContextHolder.MODE_INHERITABLETHREADLOCAL
SecurityContextHolder 还存储当前用户的信息(Authentication)
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
String username = ((UserDetails)principal).getUsername();
} else {
String username = principal.toString();
}
UserDetails 用户信息接口
获得UserDeails的接口
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
UserDetailService实现有InMemoryDaoImpl和JdbcDaoImpl等
Authentication提供的另外一个方法就是getAuthorities(),这个方法返回一个GrantedAuthority对象列表。GrantedAuthority即“角色”,如ROLE_ADMINISTRATOR或ROLE_HR_SUPERVISOR
身份验证场景做的事情:
前4项的执行:
例子:
import org.springframework.security.authentication.*;
import org.springframework.security.core.*;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
public class AuthenticationExample {
private static AuthenticationManager am = new SampleAuthenticationManager();
public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    while(true) {
    System.out.println("Please enter your username:");
    String name = in.readLine();
    System.out.println("Please enter your password:");
    String password = in.readLine();
    try {
        Authentication request = new UsernamePasswordAuthenticationToken(name, password);
        Authentication result = am.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
        break;
    } catch(AuthenticationException e) {
        System.out.println("Authentication failed: " + e.getMessage());
    }
    }
    System.out.println("Successfully authenticated. Security context contains: " +
            SecurityContextHolder.getContext().getAuthentication());
}
}
class SampleAuthenticationManager implements AuthenticationManager {
static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
static {
    AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
}
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    if (auth.getName().equals(auth.getCredentials())) {
    return new UsernamePasswordAuthenticationToken(auth.getName(),
        auth.getCredentials(), AUTHORITIES);
    }
    throw new BadCredentialsException("Bad Credentials");
}
}
负责处理AbstractSecurityInterceptor抛出的错误
通过SecurityContextPersistenceFilter来将SecurityContext作为HttpSession属性的形式存储来实现
许多无状态RestFulweb应用不存储HTTPSession,也需要依赖SecurityContextPersistenceFileter来清理Session
访问控制决定对象AccessDecisionManager
secure Object 指任何可以有security应用的对象,最常见的就是方法调用和web请求
每个受支持的secure Object 都有一个它自己的interceptor的类(作为AbstractInterceptor的子类)
AbstractSecurityInterceptor提供一下工作流来处理对象请求:
configuration Attributes 就是访问角色配置

原文:https://www.cnblogs.com/zhouyu0-0/p/12304643.html