服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring OAuth2 授权服务器配置详解

Spring OAuth2 授权服务器配置详解

2021-11-15 21:58码农小胖哥 Java教程

今天来讲讲Spring Authorization Server授权服务器的配置。强烈建议自己手动搭建一次试试,纸上得来终觉浅,深知此事要躬行。提升你的代码量才是提高编程技能的不二法门,这也是本篇教程的意义所在。

Spring OAuth2 授权服务器配置详解

前两篇文章分别体验了Spring Authorization Server的使用和讲解了其各个过滤器的作用。今天来讲讲Spring Authorization Server授权服务器的配置。强烈建议自己手动搭建一次试试,纸上得来终觉浅,深知此事要躬行。提升你的代码量才是提高编程技能的不二法门,这也是本篇教程的意义所在。

配置依赖

首先要创建一个Spring Boot Servlet Web项目,这个不难就不赘述了。集成Spring Authorization Server需要引入:

  1. -- spring security starter 必须 -->
  2. org.springframework.boot
  3. spring-boot-starter-security
  4. org.springframework.security
  5. spring-security-oauth2-authorization-server
  6. -- 截至现在版本 -->
  7. 0.2.0

OAuth2.0 Client客户端需要注册到授权服务器并持久化,Spring Authorization Server提供了JDBC实现,参见JdbcRegisteredClientRepository。为了演示方便这里我采用了H2数据库,需要以下依赖:

  1. -- jdbc 必须引入否则自行实现 -->
  2. org.springframework.boot
  3. spring-boot-starter-jdbc
  4. com.h2database
  5. h2

生产你可以切换到其它关系型数据库,数据库脚本在Spring Authorization Server入门 一文的DEMO中。

Spring Authorization Server配置

接下来是Spring Authorization Server的配置。

过滤器链配置

根据上一文对过滤器链的拆解,我们需要在Spring Security的过滤器链中注入一些特定的过滤器。这些过滤器的配置由OAuth2AuthorizationServerConfigurer来完成。以下为默认的配置:

  1. void defaultOAuth2AuthorizationServerConfigurer(HttpSecurity http) throws Exception {
  2. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  3. new OAuth2AuthorizationServerConfigurer<>();
  4. // TODO 你可以根据需求对authorizationServerConfigurer进行一些个性化配置
  5. RequestMatcher authorizationServerEndpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
  6. // 拦截 授权服务器相关的请求端点
  7. http.requestMatcher(authorizationServerEndpointsMatcher)
  8. .authorizeRequests().anyRequest().authenticated().and()
  9. // 忽略掉相关端点的csrf
  10. .csrf(csrf -> csrf.ignoringRequestMatchers(authorizationServerEndpointsMatcher))
  11. // 开启form登录
  12. .formLogin()
  13. .and()
  14. // 应用 授权服务器的配置
  15. .apply(authorizationServerConfigurer);
  16. }

你可以调用OAuth2AuthorizationServerConfigurer提供的配置方法进行一些个性化配置。

OAuth2.0客户端信息持久化

这些信息会持久化到数据库,Spring Authorization Server提供了三个DDL脚本。在入门教程的DEMO,H2会自动初始化执行这些DDL脚本,如果你切换到Mysql等数据库,可能需要你自行执行。

客户端配置信息注册

授权服务器要求客户端必须是已经注册的,避免非法的客户端发起授权申请。就像你平常去一些开放平台申请一个ClientID和Secret。下面是定义脚本:

  1. CREATE TABLE oauth2_registered_client
  2. (
  3. id varchar(100) NOT NULL,
  4. client_id varchar(100) NOT NULL,
  5. client_id_issued_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
  6. client_secret varchar(200) NULL,
  7. client_secret_expires_at timestamp NULL,
  8. client_name varchar(200) NOT NULL,
  9. client_authentication_methods varchar(1000) NOT NULL,
  10. authorization_grant_types varchar(1000) NOT NULL,
  11. redirect_uris varchar(1000) NULL,
  12. scopes varchar(1000) NOT NULL,
  13. client_settings varchar(2000) NOT NULL,
  14. token_settings varchar(2000) NOT NULL,
  15. PRIMARY KEY (id)
  16. );

对应的Java类为RegisteredClient:

  1. public class RegisteredClient implements Serializable {
  2. private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
  3. private String id;
  4. private String clientId;
  5. private Instant clientIdIssuedAt;
  6. private String clientSecret;
  7. private Instant clientSecretExpiresAt;
  8. private String clientName;
  9. private Set clientAuthenticationMethods;
  10. private Set authorizationGrantTypes;
  11. private Set redirectUris;
  12. private Set scopes;
  13. private ClientSettings clientSettings;
  14. private TokenSettings tokenSettings;
  15. // 省略
  16. }

定义一个客户端可以通过下面的Builder方法实现:

  1. RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
  2. // 唯一的客户端ID和密码
  3. .clientId("felord-client")
  4. .clientSecret("secret")
  5. // 名称 可不定义
  6. .clientName("felord")
  7. // 授权方法
  8. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  9. // 授权类型
  10. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  11. .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
  12. .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
  13. // 回调地址名单,不在此列将被拒绝 而且只能使用IP或者域名 不能使用 localhost
  14. .redirectUri("http://127.0.0.1:8080/login/oauth2/code/felord-oidc")
  15. .redirectUri("http://127.0.0.1:8080/authorized")
  16. .redirectUri("http://127.0.0.1:8080/foo/bar")
  17. .redirectUri("https://baidu.com")
  18. // OIDC支持
  19. .scope(OidcScopes.OPENID)
  20. // 其它Scope
  21. .scope("message.read")
  22. .scope("message.write")
  23. // JWT的配置项 包括TTL 是否复用refreshToken等等
  24. .tokenSettings(TokenSettings.builder().build())
  25. // 配置客户端相关的配置项,包括验证密钥或者 是否需要授权页面
  26. .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
  27. .build();

持久化到数据库的RegisteredClient用JSON表示为:

  1. {
  2. "id": "658cd010-4d8c-4824-a8c7-a86b642299af",
  3. "client_id": "felord-client",
  4. "client_id_issued_at": "2021-11-11 18:01:09",
  5. "client_secret": "{bcrypt}$2a$10$XKZ8iUckDcdQWnqw682zV.DVyGuov8Sywx1KyAn4tySsw.Jtltg0.",
  6. "client_secret_expires_at": null,
  7. "client_name": "felord",
  8. "client_authentication_methods": "client_secret_basic",
  9. "authorization_grant_types": "refresh_token,client_credentials,authorization_code",
  10. "redirect_uris": "http://127.0.0.1:8080/foo/bar,http://127.0.0.1:8080/authorized,http://127.0.0.1:8080/login/oauth2/code/felord-oidc,https://baidu.com",
  11. "scopes": "openid,message.read,message.write",
  12. "client_settings": "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"settings.client.require-proof-key\":false,\"settings.client.require-authorization-consent\":true}",
  13. "token_settings": "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"settings.token.reuse-refresh-tokens\":true,\"settings.token.id-token-signature-algorithm\":[\"org.springframework.security.oauth2.jose.jws.SignatureAlgorithm\",\"RS256\"],\"settings.token.access-token-time-to-live\":[\"java.time.Duration\",300.000000000],\"settings.token.refresh-token-time-to-live\":[\"java.time.Duration\",3600.000000000]}"
  14. }

注意上面的配置和你OAuth2.0客户端应用的配置息息相关。

既然持久化了,那自然需要操作该表的JDBC服务接口了,这个接口为RegisteredClientRepository。我们需要声明一个实现为Spring Bean,这里选择基于JDBC的实现:

  1. @Bean
  2. public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) {
  3. return new JdbcRegisteredClientRepository(jdbcTemplate);
  4. }

别忘记调用save(RegisteredClient)方法把需要注册的客户端信息持久化。

该实现依赖spring-boot-starter-jdbc类库,你也可以闲得慌使用Mybatis进行实现。

OAuth2授权信息持久化

记录授权的资源拥有者(Resource Owner)对某个客户端的某次授权记录。对应的Java类为OAuth2Authorization。下面是定义脚本:

  1. CREATE TABLE oauth2_authorization
  2. (
  3. id varchar(100) NOT NULL,
  4. registered_client_id varchar(100) NOT NULL,
  5. principal_name varchar(200) NOT NULL,
  6. authorization_grant_type varchar(100) NOT NULL,
  7. attributes varchar(4000) NULL,
  8. state varchar(500) NULL,
  9. authorization_code_value blob NULL,
  10. `authorization_code_issued_at` timestamp NULL,
  11. authorization_code_expires_at timestamp NULL,
  12. authorization_code_metadata varchar(2000) NULL,
  13. access_token_value blob NULL,
  14. access_token_issued_at timestamp NULL,
  15. access_token_expires_at timestamp NULL,
  16. access_token_metadata varchar(2000) NULL,
  17. access_token_type varchar(100) NULL,
  18. access_token_scopes varchar(1000) NULL,
  19. oidc_id_token_value blob NULL,
  20. oidc_id_token_issued_at timestamp NULL,
  21. oidc_id_token_expires_at timestamp NULL,
  22. oidc_id_token_metadata varchar(2000) NULL,
  23. refresh_token_value blob NULL,
  24. refresh_token_issued_at timestamp NULL,
  25. refresh_token_expires_at timestamp NULL,
  26. refresh_token_metadata varchar(2000) NULL,
  27. PRIMARY KEY (id)
  28. );

这里的机制目前还没有研究,先挖个坑。

同样它也需要一个持久化服务接口OAuth2AuthorizationService并注入Spring IoC:

  1. /**
  2. * 管理OAuth2授权信息服务
  3. *
  4. * @param jdbcTemplate the jdbc template
  5. * @param registeredClientRepository the registered client repository
  6. * @return the o auth 2 authorization service
  7. */
  8. @Bean
  9. public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate,
  10. RegisteredClientRepository registeredClientRepository) {
  11. return new JdbcOAuth2AuthorizationService(jdbcTemplate,
  12. registeredClientRepository);
  13. }

持久化到数据库的OAuth2Authorization用JSON表示为:

  1. {
  2. "id": "aa2f6e7d-d9b9-4360-91ef-118cbb6d4b09",
  3. "registered_client_id": "658cd010-4d8c-4824-a8c7-a86b642299af",
  4. "principal_name": "felord",
  5. "authorization_grant_type": "authorization_code",
  6. "attributes": "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest\":{\"@class\":\"org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest\",\"authorizationUri\":\"http://localhost:9000/oauth2/authorize\",\"authorizationGrantType\":{\"value\":\"authorization_code\"},\"responseType\":{\"value\":\"code\"},\"clientId\":\"felord-client\",\"redirectUri\":\"http://127.0.0.1:8080/foo/bar\",\"scopes\":[\"java.util.Collections$UnmodifiableSet\",[\"message.read\",\"message.write\"]],\"state\":\"9gTcVNXgV8Pn_Ron3bkFb6M92AYCodeWKoEd6xxaiUg=\",\"additionalParameters\":{\"@class\":\"java.util.Collections$UnmodifiableMap\"},\"authorizationRequestUri\":\"http://localhost:9000/oauth2/authorize?response_type=code&client_id=felord-client&scope=message.read%20message.write&state=9gTcVNXgV8Pn_Ron3bkFb6M92AYCodeWKoEd6xxaiUg%3D&redirect_uri=http://127.0.0.1:8080/foo/bar\",\"attributes\":{\"@class\":\"java.util.Collections$UnmodifiableMap\"}},\"java.security.Principal\":{\"@class\":\"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\",\"authorities\":[\"java.util.Collections$UnmodifiableRandomAccessList\",[{\"@class\":\"org.springframework.security.core.authority.SimpleGrantedAuthority\",\"authority\":\"ROLE_USER\"}]],\"details\":{\"@class\":\"org.springframework.security.web.authentication.WebAuthenticationDetails\",\"remoteAddress\":\"0:0:0:0:0:0:0:1\",\"sessionId\":\"FD624F1AD55A2418CC9815A86AA32696\"},\"authenticated\":true,\"principal\":{\"@class\":\"org.springframework.security.core.userdetails.User\",\"password\":null,\"username\":\"felord\",\"authorities\":[\"java.util.Collections$UnmodifiableSet\",[{\"@class\":\"org.springframework.security.core.authority.SimpleGrantedAuthority\",\"authority\":\"ROLE_USER\"}]],\"accountNonExpired\":true,\"accountNonLocked\":true,\"credentialsNonExpired\":true,\"enabled\":true},\"credentials\":null},\"org.springframework.security.oauth2.server.authorization.OAuth2Authorization.AUTHORIZED_SCOPE\":[\"java.util.Collections$UnmodifiableSet\",[\"message.read\",\"message.write\"]]}",
  7. "state": null,
  8. "authorization_code_value": "EZFxDcsKoaGtyqRTS0oNMg85EcVcyLdVssuD3SV-o0FvNXsSTRjTmCdu0ZPZnVIQ7K4TTSzrvLwBqoRXOigo_dWVNeqE44LjHHL_KtujM_Mxz8hLZgGhtfipvTdpWWR1",
  9. "authorization_code_issued_at": "2021-11-11 18:44:45",
  10. "authorization_code_expires_at": "2021-11-11 18:49:45",
  11. "authorization_code_metadata": "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"metadata.token.invalidated\":true}",
  12. "access_token_value": "eyJ4NXQjUzI1NiI6IlZGR1F4Q21nSEloX2dhRi13UGIxeEM5b0tBMXc1bGEwRUZtcXFQTXJxbXciLCJraWQiOiJmZWxvcmRjbiIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJmZWxvcmQiLCJhdWQiOiJmZWxvcmQtY2xpZW50IiwibmJmIjoxNjM2NjI3NDg0LCJzY29wZSI6WyJtZXNzYWdlLnJlYWQiLCJtZXNzYWdlLndyaXRlIl0sImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdDo5MDAwIiwiZXhwIjoxNjM2NjI3Nzg0LCJpYXQiOjE2MzY2Mjc0ODR9.CFzye9oIh8-ZMpyp9XoIXIQLnj2Sn17yZ9bgn7NYAbrp2hRq-Io_Se2SJpSEMa_Ce44aOGmcLTmIOILYUxlU08QCtHgr4UfCZttzroQhEn3Qui7fixBMprPYqxmu2KL5G_l3q5EWyh4G0ilHpByCBDeBGAl7FpaxSDlelnBfNGs9q6nJCs7aC40U_YPBRLoCBLVK1Y8t8kQvNu8NqCkS5D5DZAogpmlVg7jSIPz1UXVIh7iDTTQ1wJl6rZ1E87E0UroX4eSuYfMQ351y65IUlB14hvKhu03yDLTiVKtujOo3m0DAkJTbk3ZkFZEmDf4N3Yn-ktU7cyswQWa1bKf3og",
  13. "access_token_issued_at": "2021-11-11 18:44:45",
  14. "access_token_expires_at": "2021-11-11 18:49:45",
  15. "access_token_metadata": "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"metadata.token.claims\":{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"sub\":\"felord\",\"aud\":[\"java.util.Collections$SingletonList\",[\"felord-client\"]],\"nbf\":[\"java.time.Instant\",1636627484.674000000],\"scope\":[\"java.util.Collections$UnmodifiableSet\",[\"message.read\",\"message.write\"]],\"iss\":[\"java.net.URL\",\"http://localhost:9000\"],\"exp\":[\"java.time.Instant\",1636627784.674000000],\"iat\":[\"java.time.Instant\",1636627484.674000000]},\"metadata.token.invalidated\":false}",
  16. "access_token_type": "Bearer",
  17. "access_token_scopes": "message.read,message.write",
  18. "oidc_id_token_value": null,
  19. "oidc_id_token_issued_at": null,
  20. "oidc_id_token_expires_at": null,
  21. "oidc_id_token_metadata": null,
  22. "refresh_token_value": "hbD9dVMpu855FhDDOYapwsQSx8zO9iPX5LUZKeXWzUcbE2rgYRV-sgXl5vGwyByLNljcqVyK9Pgquzbcoe6dkt0_yPQPJfxLY8ezEQ-QREBjxNYqecd6OI9SHMQkBObG",
  23. "refresh_token_issued_at": "2021-11-11 18:44:45",
  24. "refresh_token_expires_at": "2021-11-11 19:44:45",
  25. "refresh_token_metadata": "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"metadata.token.invalidated\":false}"
  26. }

存储的东西还是比较全的,甚至把Java类都序列化了。

确认授权持久化

资源拥有者(Resource Owner)对授权的确认信息OAuth2AuthorizationConsent的持久化,这个比较简单。下面是定义脚本:

  1. CREATE TABLE oauth2_authorization_consent
  2. (
  3. registered_client_id varchar(100) NOT NULL,
  4. principal_name varchar(200) NOT NULL,
  5. authorities varchar(1000) NOT NULL,
  6. PRIMARY KEY (registered_client_id, principal_name)
  7. );

对应的持久化服务接口为OAuth2AuthorizationConsentService,也要注入Spring IoC:

  1. @Bean
  2. public OAuth2AuthorizationConsentService authorizationConsentService(JdbcTemplate jdbcTemplate,
  3. RegisteredClientRepository registeredClientRepository) {
  4. return new JdbcOAuth2AuthorizationConsentService(jdbcTemplate, registeredClientRepository);
  5. }

持久化到数据库的OAuth2AuthorizationConsent用JSON表示为:

  1. {
  2. "registered_client_id": "658cd010-4d8c-4824-a8c7-a86b642299af",
  3. "principal_name": "felord",
  4. "authorities": "SCOPE_message.read,SCOPE_message.write"
  5. }

JWK

JWK全称JSON Web Key,是一个将加密的密钥用JSON对象描述的规范,和JWT一样是JOSE规范的重要组成部分。规范的详细定义可参考JWK文档。JWK参考示例:

  1. {
  2. "keys": [
  3. {
  4. "kty": "RSA",
  5. "x5t#S256": "VFGQxCmgHIh_gaF-wPb1xC9oKA1w5la0EFmqqPMrqmw",
  6. "e": "AQAB",
  7. "kid": "felordcn",
  8. "x5c": [
  9. "MIIDczCCAlugAwIBAgIEURwmYzANBgkqhkiG9w0BAQsFADBqMQ0wCwYDVQQGEwQoY24pMQ0wCwYDVQQIEwQoaG4pMQ0wCwYDVQQHEwQoenopMRMwEQYDVQQKEwooZmVsb3JkY24pMRMwEQYDVQQLEwooZmVsb3JkY24pMREwDwYDVQQDEwgoRmVsb3JkKTAeFw0yMTEwMTgwNDUwMTRaFw0yMjEwMTgwNDUwMTRaMGoxDTALBgNVBAYTBChjbikxDTALBgNVBAgTBChobikxDTALBgNVBAcTBCh6eikxEzARBgNVBAoTCihmZWxvcmRjbikxEzARBgNVBAsTCihmZWxvcmRjbikxETAPBgNVBAMTCChGZWxvcmQpMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgo0TPk1td7iROmmLcGbOsZ2F68kTertDwRyk+leqBl+qyJAkjoVgVaCRRQHZmvu/YGp93vOaEd/zFdVj/rFvMXmwBxgYPOeSG0bHkYtFBaUiLf1vhW5lyiPHcGide3uw1p+il3JNiOpcnLCbAKZgzm4qaugeuOD02/M0YcMW2Jqg3SUWpC+9vu9yt5dVc1xpmpwEAamKzvynI3Zxl44ddlA8RRAS6kV0OUcKbEG63G3yZ4MHnhrFrZDuvlwfSSgn0wFOC/b6mJ+bUxByMAXKD0d4DS2B2mVl7RO5AzL4SFcqtZZE3Drtcli67bsENyOQeoTVaKO6gu5PEEFlQ7pHKwIDAQABoyEwHzAdBgNVHQ4EFgQUqbLZ76DtdEEpTZUcgP7LsdGk8/AwDQYJKoZIhvcNAQELBQADggEBAEzfhi6/B00NxSPKAYJea/0MIyHr4X8Ue6Du+xl2q0UFzSkyIiMcPNmOYW5L1PWGjxR5IIiUgxKI5bEvhLkHhkMV+GRQSPKJXeC3szHdoZ3fXDZnuC0I88a1YDsvzuVhyjLL/n5lETRT4QWo5LsAj5v7AX+p46HM0iqSyTptafm2wheEosFA3ro4+vEDRaMrKLY1KdJuvvrrQIRpplhB/JbncmjWrEEvICSLEXm+kdGFgWNXkNxF0PhTLyPu3tEb2xLmjFltALwi3KPUGv9zVjxb7KyYiMnVsOPnwpDLOyREM9j4RLDiwf0tsCqPqltVExvFZouoL26fhcozfcrqC70="
  10. ],
  11. "n": "go0TPk1td7iROmmLcGbOsZ2F68kTertDwRyk-leqBl-qyJAkjoVgVaCRRQHZmvu_YGp93vOaEd_zFdVj_rFvMXmwBxgYPOeSG0bHkYtFBaUiLf1vhW5lyiPHcGide3uw1p-il3JNiOpcnLCbAKZgzm4qaugeuOD02_M0YcMW2Jqg3SUWpC-9vu9yt5dVc1xpmpwEAamKzvynI3Zxl44ddlA8RRAS6kV0OUcKbEG63G3yZ4MHnhrFrZDuvlwfSSgn0wFOC_b6mJ-bUxByMAXKD0d4DS2B2mVl7RO5AzL4SFcqtZZE3Drtcli67bsENyOQeoTVaKO6gu5PEEFlQ7pHKw"
  12. }
  13. ]
  14. }

JWK的意义在于生成JWT和提供JWK端点给OAuth2.0资源服务器解码校验JWT。

公私钥

JWK会涉及到加密算法,这里使用RSASHA256算法来作为加密算法,并通过Keytool工具来生成.jks公私钥证书文件。当然你也可以通过openssl来生成pkcs12格式的证书。在Spring Security实战干货中已经对生成的方法进行了说明,这里不再赘述。

JWKSource

由于Spring Security的JOSE实现依赖的是nimbus-jose-jwt,所以这里只需要我们实现JWKSource 并注入Spring IoC即可。相关代码如下:

  1. /**
  2. * 加载JWK资源
  3. *
  4. * @return the jwk source
  5. */
  6. @SneakyThrows
  7. @Bean
  8. public JWKSource jwkSource() {
  9. //TODO 这里优化到配置
  10. // 证书的路径
  11. String path = "felordcn.jks";
  12. // 证书别名
  13. String alias = "felordcn";
  14. // keystore 密码
  15. String pass = "123456";
  16. ClassPathResource resource = new ClassPathResource(path);
  17. KeyStore jks = KeyStore.getInstance("jks");
  18. KeyStore pkcs12 = KeyStore.getInstance("pkcs12");
  19. char[] pin = pass.toCharArray();
  20. jks.load(resource.getInputStream(), pin);
  21. RSAKey rsaKey = RSAKey.load(jks, alias, pin);
  22. JWKSet jwkSet = new JWKSet(rsaKey);
  23. return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
  24. }

授权服务器元信息配置

客户端信息RegisteredClient包含了Token的配置项TokenSettings和客户端配置项ClientSettings。授权服务器本身也提供了一个配置工具来配置其元信息,大多数我们都使用默认配置即可,唯一需要配置的其实只有授权服务器的地址issuer,在DEMO中虽然我使用localhost:9000了issuer没有什么问题,但是在生产中这个地方应该配置为域名。

  1. /**
  2. * 配置 OAuth2.0 provider元信息
  3. *
  4. * @return the provider settings
  5. */
  6. @Bean
  7. public ProviderSettings providerSettings(@Value("${server.port}") Integer port) {
  8. //TODO 生产应该使用域名
  9. return ProviderSettings.builder().issuer("http://localhost:" + port).build();
  10. }

你可以修改本地的hosts文件试试用域名。

到这里Spring Authorization Server的配置就完成了,但是整个授权服务器的配置还没有完成。

授权服务器安全配置

上面是授权服务器本身的配置,授权服务器本身的安全配置是另外一条过滤器链承担的,我们也要对它进行一些配置,都是常规的Spring Security配置,这里给一个简单的配置,也是DEMO中的配置:

  1. @EnableWebSecurity(debug = true)
  2. public class DefaultSecurityConfig {
  3. // @formatter:off
  4. @Bean
  5. SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
  6. http.authorizeRequests(authorizeRequests ->
  7. authorizeRequests.anyRequest().authenticated()
  8. )
  9. .formLogin();
  10. return http.build();
  11. }
  12. // @formatter:on
  13. /**
  14. * 在内存中抽象一个Spring Security安全用户{@link User},同时该用户也是Resource Owner;
  15. * 实际开发中需要持久化到数据库。
  16. *
  17. * @return the user details service
  18. */
  19. // @formatter:off
  20. @Bean
  21. UserDetailsService users() {
  22. UserDetails user = User.builder()
  23. .username("felord")
  24. .password("password")
  25. .passwordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder()::encode)
  26. .roles("USER")
  27. .build();
  28. return new InMemoryUserDetailsManager(user);
  29. }
  30. // @formatter:on
  31. /**
  32. * 开放一些端点的访问控制。
  33. *
  34. * 如果你使用了一些依赖这些端点的程序,比如Consul健康检查;
  35. * 打开H2数据库web控制台访问控制,方便你查看数据具体看配置文件说明。
  36. *
  37. * @return the web security customizer
  38. */
  39. @Bean
  40. WebSecurityCustomizer webSecurityCustomizer() {
  41. return web -> web.ignoring().antMatchers("/actuator/health","/h2-console/**");
  42. }
  43. }

到这里一个基于Spring Authorization Server的授权服务器就搭建好了。下一篇我们将实现OAuth2.0的登录功能,敬请期待。

解惑

为什么一个项目配置了两个甚至多个SecurityFilterChain?

之所以有两个SecurityFilterChain是因为程序设计要保证职责单一,无论是底层架构还是业务代码,为此HttpSecurity被以基于原型(prototype)的Spring Bean注入Spring IoC。针对本应用中的两条过滤器链,分别是授权服务器的过滤器链和应用安全的过滤器链,它们之间其实互相没有太多联系。

原文链接:https://mp.weixin.qq.com/s/eIYhh1cvGMfA7ancMJ6VeA

延伸 · 阅读

精彩推荐