介绍
今天就跟大家聊聊有关如何在春天中使用安全实现单点登录,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
<强>关键依赖强>
& lt; parent> ,,,& lt; groupId> org.springframework.boot ,,,& lt; artifactId> spring-boot-starter-parent ,,,& lt; version> 2.1.2.RELEASE ,,,& lt; relativePath/比; & lt;/parent> & lt; dependencies> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework.boot ,,,,,& lt; artifactId> spring-boot-starter-security ,,,& lt;/dependency> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework.boot ,,,,,& lt; artifactId> spring-boot-starter-web ,,,& lt;/dependency> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework.boot ,,,,,& lt; artifactId> spring-boot-starter-test ,,,,,& lt; scope> test ,,,& lt;/dependency> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework.security ,,,,,& lt; artifactId> spring-security-test ,,,,,& lt; scope> test ,,,& lt;/dependency> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework.security.oauth.boot ,,,,,& lt; artifactId> spring-security-oauth3-autoconfigure ,,,,,& lt; version> 2.1.2.RELEASE ,,,& lt;/dependency> & lt;/dependencies>
<>强认证服务器强>
认证服务器的关键代码有如下几个文件:
<代码> AuthServerApplication 代码>:
@SpringBootApplication @EnableResourceServer public class  AuthServerApplication { public 才能;static  void main (String [], args), { ,,,SpringApplication.run (AuthServerApplication.class, args); ,,} }
<代码> AuthorizationServerConfiguration 代码>认证配置:
@ configuration @EnableAuthorizationServer class AuthorizationServerConfiguration  extends AuthorizationServerConfigurerAdapter  { @ autowired才能 AuthenticationManager 才能;authenticationManager; @ autowired才能 TokenStore 才能;tokenStore; @ autowired才能 BCryptPasswordEncoder 才能;编码器; @Override才能 public 才能;void 配置(ClientDetailsServiceConfigurer 客户),throws Exception { ,,,//配置客户端 ,,,的客户 ,,,,,,,.inMemory () ,,,,,,,.withClient (“client") ,,,,,,,.secret (encoder.encode (“123456“)) .resourceIds (“hi") ,,,,,,,.authorizedGrantTypes (“password",“refresh_token") ,,,,,,,.scopes (“read"); ,,} @Override才能 public 才能;void 配置(AuthorizationServerEndpointsConfigurer 端点),throws Exception { ,,,的端点 ,,,,,,,.tokenStore (tokenStore) ,,,,,,,.authenticationManager (authenticationManager); ,,} @Override才能 public 才能;void 配置(AuthorizationServerSecurityConfigurer oauthServer), throws Exception { ,,,//允许表单认证 ,,oauthServer ,,,,,,,.allowFormAuthenticationForClients () ,,,,,,,.checkTokenAccess (“permitAll ()“) ,,,,,,,.tokenKeyAccess (“permitAll ()“); ,,} }
代码中配置了一个客户,id是<代码>客户端> 代码,密码<代码> 123456 代码>。<代码> authorizedGrantTypes> 代码有密码<代码> 代码>和<代码> refresh_token> 代码两种方式。
<代码> SecurityConfiguration 代码>安全配置:
@ configuration @EnableWebSecurity public class  SecurityConfiguration extends WebSecurityConfigurerAdapter  { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在春天中使用安全实现单点登录