我创建了一个授权服务,如下所示
@SpringBootApplication
@EnableAuthorizationServer
public class AuthorizationApplication {
...
}
Run Code Online (Sandbox Code Playgroud)
有了这个application.properties.
server.port=9000
security.oauth2.client.client-id=monederobingo
security.oauth2.client.client-secret=monederobingosecret
security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentials
security.oauth2.client.scope=company,client
Run Code Online (Sandbox Code Playgroud)
然后,在一个单独的Spring启动项目中,我创建了一个资源服务器.
@SpringBootApplication
@EnableResourceServer
public class App {
...
}
Run Code Online (Sandbox Code Playgroud)
有了这个application.properties.
server.port=9090
spring.application.name=app
security.oauth2.resource.user-info-uri=http://localhost:9000/user
Run Code Online (Sandbox Code Playgroud)
现在,如果我localhost:9090/api使用由授权服务检索的相应令牌发送此类请求,一切正常.
但是,我不想在发送请求时发送此令牌localhost:9090/login.
为此我在Resource server spring boot app中创建了这个类.
@Configuration
public class SpringConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/login")
.permitAll()
.antMatchers("/api/**")
.authenticated();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我不需要发送任何令牌来发送请求/login.
但是,我现在正在/api使用有效令牌发送请求时发出以下消息.
{
"timestamp": 1496027102659,
"status": 403,
"error": "Forbidden",
"message": …Run Code Online (Sandbox Code Playgroud) 我使用Spring Boot版本1.5.2.RELEASE实现了oAuth2授权服务器。授权服务器支持隐式流。使用登录表单(http:// localhost:8200 / login)下面的WebSecurityConfig 可以很好地工作。
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JpaUserDetailsService userDetailsService;
@Bean
@Override
public UserDetailsService userDetailsServiceBean() throws Exception {
return userDetailsService;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationProvider authenticationProvider() throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsServiceBean());
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return new ProviderManager(singletonList(authenticationProvider()));
}
@Override
public void configure(WebSecurity web) {
web.ignoring()
.antMatchers("/")
.antMatchers("/docs/**")
.antMatchers("/swagger/**")
.antMatchers("/token/**")
.antMatchers("/v2/*") …Run Code Online (Sandbox Code Playgroud) spring-security spring-boot spring-security-oauth2 spring-oauth2
对于我的 REST API,我使用 JWT 进行 OAuth2 授权。目前我正在扩展JwtTokenStore以将刷新令牌存储在内存中,以便我能够撤销它们。
// TODO: This is a temporary in memory solution that needs to be replaced with a concrete persistent implementation.
public class MyJwtTokenStore extends JwtTokenStore {
private List<OAuth2RefreshToken> refreshTokens;
public MyJwtTokenStore(JwtAccessTokenConverter jwtTokenEnhancer) {
super(jwtTokenEnhancer);
refreshTokens = new ArrayList<>();
}
@Override
public OAuth2RefreshToken readRefreshToken(String tokenValue) {
OAuth2RefreshToken refreshToken = super.readRefreshToken(tokenValue);
if (!refreshTokens.contains(refreshToken)) {
throw new InvalidGrantException("Invalid refresh token: " + tokenValue);
}
return refreshToken;
}
@Override
public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 facebook 登录设置一个项目,并在 connexion 上检索用户的一些个人资料信息。
语境:
一切正常,我能够通过 facebook 连接用户,我的问题是 facebook 只检索我的 id (facebook id) 和用户名。我想在登录时从 Facebook 获得更多信息,例如个人资料网址和个人资料图片。
安全配置:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.oauth2Login();
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性:
spring.security.oauth2.client.registration.facebook.client-id=app-id
spring.security.oauth2.client.registration.facebook.client-secret=app-secret
Run Code Online (Sandbox Code Playgroud)
我什至尝试在 application.properties 上添加范围,但没有任何改变
spring.security.oauth2.client.registration.facebook.scope=public_profile,email
Run Code Online (Sandbox Code Playgroud)
在 facebook 文档中,他们说
public_profile、user_friends 和 email 是公共范围,不需要任何验证
这有什么帮助吗?
谢谢。
我目前正在检查AuthorizationJWT令牌值的所有传入标头,以便我可以检查这些令牌是否被列入黑名单.我Filter为此实现了一个自定义.
问题是此过滤器正在处理进入服务器的所有请求.我只想处理到/oauth/token端点的请求.
是否可以在Spring OAuth2 Auth Server中实现它?
我创建了三个应用程序“spring cloud gateway(8081)”、“spring oauth2 auth server(8094)”和“spring oauth2 Resource server(8097)”。
\n\n当我想请求资源服务器时,首先我需要请求 gw,它转发到 oauth 服务器,然后我登录那里(oauth 也有 spring 安全层)。成功登录后,它会重定向到后台 gw 服务器,例如http://localhost:8081/login/oauth2/code/gateway?code=6ldKVF&state=0WvvWdTs8G_XchSTQKqgokua_XDVQziqVZ_VXLMqIS0%3D url。然后,屏幕上发生错误。
\n\n当我在身份验证服务器成功登录时,网关服务器控制台中有一个跟踪日志:
\n\n\n\n\n2020-01-17 17:52:11.825 跟踪 11336 --- [ctor-http-nio-4]\n oshttp.codec.json.Jackson2JsonDecoder :[21762c89] 已解码\n [{access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOls ib2F1dGgyLXJlc291cmNlIl0sInVzZXJfbmFtZSI6ImRnIiwic2NvcGUIOlsiY3VzdG9tX21vZCJdLCJleHAiOjE1NzkyNzI3NDEsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiowu zYzQ2YTQtMDJiZi00MTgwLTg1ZTktMGJhOTM0MjBhYjg4IiwiY2xpZW50X2lkIjoiZmlyc3QtY2xpZW50In0.xdWGm420tvp2Rzq0AyCgOTcDuKvP- V6JFd76KmJJf7o,\n token_type=承载,\n refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsib2F1dGgyLXJlc291cmNlIl0sInVzZXJfbmFtZSI6ImRnIiwic2NvcGUiOlsiY3Vz dG9tX21vZCJdLCJhdGkiOiI5ZTNjNDZhNC0wMmJmLTQxODAtODVLOS0wYmE5MzQyMGFiODgiLCJleHAiOjE1NzkyNzI3NTEsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiYzeyNWexM2It MmMzYS00ZGM0LWJjODgtZDc4ZDk1ZTljNzQ5IiwiY2xpZW50X2lkIjoiZmlyc3QtY2xpZW50In0.KhRIy7wOH2IsswDZ_AIXVFdtu6JZqtiLBZGZIypeNRw,\n expires_in=9,scope=custom_mod,\n jti=9e3 c46a4-02bf-4180-85e9-0ba93420ab88}]
\n
当我解码以访问令牌 jwt 时,结果如下。
\n\n{\n"aud": [\n"oauth2-resource"\n],\n"user_name": "dg",\n"scope": [\n"custom_mod"\n],\n"exp": 1579272741,\n"authorities": [\n"ROLE_ADMIN"\n],\n"jti": "9e3c46a4-02bf-4180-85e9-0ba93420ab88",\n"client_id": "first-client"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我想,我缺少一些在网关服务器或 oauth 服务器中实现的部分,但我找不到。因为当我尝试使用 okta 而不是我的自定义身份验证服务器时,没有错误。
\n\n网关应用程序.properties
\n\nserver.port=8081\nspring.security.oauth2.client.registration.gateway.client-id=first-client\nspring.security.oauth2.client.registration.gateway.client-secret=noonewilleverguess\nspring.security.oauth2.client.registration.gateway.authorization-grant-type=authorization_code\nspring.security.oauth2.client.registration.gateway.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}\n\nspring.security.oauth2.client.provider.gateway.authorization-uri=http://localhost:8094/oauth/authorize\nspring.security.oauth2.client.provider.gateway.token-uri=http://localhost:8094/oauth/token?scope=custom_mod\nspring.security.oauth2.client.provider.gateway.user-info-uri=http://localhost:8094/userinfo\nspring.security.oauth2.client.provider.gateway.user-name-attribute=name\n\nlogging.level.root=trace\nRun Code Online (Sandbox Code Playgroud)\n\n这是屏幕错误消息:
\n\n\n1 月 17 日星期五 17:28:11 EET 2020 …
spring-security spring-security-oauth2 spring-cloud spring-oauth2 spring-cloud-gateway
我正在尝试使用 spring-boot 设置 oauth2 客户端。我对我的有这个依赖pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
我的这个配置application.properties:
spring.security.oauth2.client.registration.mercadolivre.client-id=...
spring.security.oauth2.client.registration.mercadolivre.client-secret=...
Run Code Online (Sandbox Code Playgroud)
和这个安全配置类:
@Configuration
public class Security extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行应用程序时,给我这个错误:
Caused by: java.lang.IllegalStateException: Provider ID must be specified for client registration 'mercadolivre'
at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getBuilder(OAuth2ClientPropertiesRegistrationAdapter.java:95) ~[spring-boot-autoconfigure-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getClientRegistration(OAuth2ClientPropertiesRegistrationAdapter.java:61) ~[spring-boot-autoconfigure-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.lambda$getClientRegistrations$0(OAuth2ClientPropertiesRegistrationAdapter.java:53) ~[spring-boot-autoconfigure-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
at java.base/java.util.HashMap.forEach(HashMap.java:1338) ~[na:na]
at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(OAuth2ClientPropertiesRegistrationAdapter.java:52) …Run Code Online (Sandbox Code Playgroud)