Spring Security 实现身份认证

Spring Security可以运行在不同的身份认证环境中,当我们推荐用户使用Spring Security进行身份认证但并不推荐集成到容器管理的身份认证中时,但当你集成到自己的身份认证系统时,它依然是支持的。

1. Spring Security中的身份认证是什么?

现在让我们考虑一下每个人都熟悉的标准身份认证场景:

(1)用户打算使用用户名和密码登陆系统

(2)系统验证用户名和密码合法

(3)得到用户信息的上下文(角色等信息)

(4)为用户建立一个安全上下文

(5)用户接下来可能执行一些权限访问机制下的受保护的操作,检查与当前安全上下文有关的必须的权限

上面前三步是身份认证的过程,接下来看看身份认证的详细过程:

(1)用户名和密码获得之后组合成UsernamePasswordAuthenticationToken的实例(前文讨论过的Authentication接口的实例)

(2)将该令牌传递给AuthenticationManager实例进行验证

(3)验证成功后,AuthenticationManager 会返回填充好的Authentication实例

(4)通过调用SecurityContextHolder.getContext().setAuthentication(…)建立安全上下文的实例,传递到返回的身份认证对象上

下面是进行身份认证的代码片段:

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");}}

我们写了一个小程序,要求用户输入用户名和密码并执行上述序列。我们实现的 AuthenticationManager 会验证用户名和密码是否一致,它分配了一个角色给每个用户。上面的输出类似于这样:

Please enter your username:

favboy

Please enter your password:

favccxx

Authentication failed:Bad Credentials

Please enter your username:

favboy

Please enter your password:

favboy

Successfully authenticated. Security context contains: \

org.springframework.security.authentication.UsernamePasswordAuthenticationToken@441d0230: \

Principal: bob; Password: [PROTECTED]; \

Authenticated: true; Details: null; \

Granted Authorities: ROLE_USER

注意,你通常不需要写任何代码。这个过程通常发生在内部,如web身份认证过滤器。上面的代码仅仅是告诉我们在Spring Security中使用身份认证是如此简单的事情。当 SecurityContextHolder 包含一个填充的 Authentication 对象时用户身份就完成了。

2. 直接设置 SecurityContextHolder的内容

实际上,Spring Security并不关心如何将 Authentication对象放到SecurityContextHolder中。唯一的关键就是 SecurityContextHolder需要在用户操作认证的 AbstractSecurityInterceptor 之前已经有了Authentication对象。

蚁穴虽小,溃之千里。

Spring Security 实现身份认证

相关文章:

你感兴趣的文章:

标签云: