单点登录系统知识点总结( 四 )


@Bean@Overridepublic AuthenticationManager authenticationManager() throws Exception {return super.authenticationManager();}

单点登录系统知识点总结

文章插图
3.代码实现
package com.jt.auth.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.oauth2.provider.token.TokenStore;import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;@Configurationpublic class TokenConfig {/*** 定义令牌存储的方案,本次选择基于JWT令牌方式存储用户状态* */@Beanpublic TokenStore tokenStore(){//这里采用JWT方式生成和存储令牌信息return new JwtTokenStore(jwtAccessTokenConverter());}/*** 配置JWT令牌和解析对象* */@Beanpublic JwtAccessTokenConverter jwtAccessTokenConverter() {JwtAccessTokenConverter converter = new JwtAccessTokenConverter();converter.setSigningKey(SIGNING_KEY);//设置密钥return converter;}/*** JWT 令牌签名时使用的密钥(可以理解为盐值加密中的盐)* 1)生成的令牌需要这个密钥进行签名* 2)获取的令牌需要使用这个密钥进行验签(校验令牌合法性,是否被篡改过)*///这里的签名key将来可以写到配置中心private static final String SIGNING_KEY="auth";}
4.认证授权配置代码实现
适配器模式:不需要重写方法,适配器充当一个转化的作用,需要哪个方法,在进行重写 。
//AuthorizationServerConfigurerAdapter认证授权的适配器——用到了适配器模式//因此Oauth2Config继承这个接口,不需要重写全部的方法,需要哪个方法在进行定义即可@AllArgsConstructor //生成一个全参构造函数@EnableAuthorizationServer //启动认证和授权@Configurationpublic class Oauth2Config extends AuthorizationServerConfigurerAdapter {//为下文需要调用的进行注入 。//1.可以给每个上面都加入@Autowired//2.加入一个全参构造函数,或者在类上面加一个@AllArgsConstructor //生成一个全参构造函数private AuthenticationManager authenticationManager;//在SecurityConfig@Bean一个authenticationManager()private UserDetailsServiceImpl userDetailsService;//调用的是service层中的UserDetailsServiceImpl类private TokenStore tokenStore;//在TokenConfig@Bean tokenStore()private PasswordEncoder passwordEncoder;//在SecurityConfig @BeanpasswordEncoder()加密的算法private JwtAccessTokenConverter jwtAccessTokenConverter;//在TokenConfig @Bean}
(1.)配置认证规则
@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {//super.configure(endpoints);endpoints//配置由谁完成认证?(认证管理器).authenticationManager(authenticationManager)//谁负责查询用户业务数据(可写可不写 。认证时需要两部分信息:一部分来自客户端,一部分来自数据库).userDetailsService(userDetailsService)//支持对什么请求进行认证(可写可不写 。默认支持post方式).allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST)//认证成功以后令牌如何生成和存储?(若不写默认生成普通令牌UUID.randomUUID(),存储方式为内存).tokenServices(tokenService());}@Beanpublic AuthorizationServerTokenServices tokenService() {//1.创建授权令牌服务对象(TokenServices)DefaultTokenServices tokenServices =new DefaultTokenServices();//2.配置令牌的存储(tokenStore)tokenServices.setTokenStore(tokenStore);//3.配置令牌增强(不进行配置,就是默认普通令牌,使用的就是UUID)TokenEnhancerChain tokenEnhancer=new TokenEnhancerChain();tokenEnhancer.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter));tokenServices.setTokenEnhancer(tokenEnhancer);//4.设置令牌的有效时间tokenServices.setAccessTokenValiditySeconds(3600);//1小时//5.设置令牌刷新策略(是否支持使用刷新令牌在生成新令牌)tokenServices.setSupportRefreshToken(true);//6.设置刷新令牌有效时长tokenServices.setRefreshTokenValiditySeconds(3600*5);//5小时return tokenServices;}