spring security和java web token整合

思路:

spring security

1、用户输入用户名密码。

2、验证:从库中(可以是内存、数据库等)查询该用户的密码、角色,验证用户名和密码是否正确。如果正确,则将填充Authentication(UsernamePasswordAuthenticationToken是其实现类),填充Authentication,会将用户的角色也填充进去。

Authentication authentication= new UsernamePasswordAuthenticationToken(auth.getName(),
        auth.getCredentials(), AUTHORITIES);//设置用户名、密码、权限列表创建Authentication对象,AUTHORITIES为权限列表

3、授权:根据WebSecurityConfigurerAdapter中的configure(HttpSecurity httpSecurity)配置,FilterSecurityInterceptor会 判断此用户是否可以访问handler。如果没有权限,则会抛出异常。

4、SecurityContextHolder会管理填充好的Authentication,SecurityContextHolder利用ThreadLocal管理这些安全对象,这样在逻辑代码中随时取出用户的信息。

java web token

参考:https://www.cnblogs.com/BonnieWss/p/11129400.html

spring security与jwt整合

1、从请求头中取出jwt,并解析,如果能正确解析,说明可以通过验证,并取出其负载信息UserDetails

2、将UserDetails填充Authentication

Authentication authentication= new UsernamePasswordAuthenticationToken(userDetails,
        userDetails.getCredentials(), userDetails.getAuthorities);//设置用户名、密码、权限列表创建Authentication对象,AUTHORITIES为权限列表

3、后续步骤和spring security一样

具体代码参考:https://www.cnblogs.com/BonnieWss/p/11149699.html

原文地址:https://www.cnblogs.com/BonnieWss/p/11359023.html