spring - Spring Security 不适用于授权:来自 OAuth2 的承载令牌
问题描述
我有两个微服务,第一个用于 OAuth2,第二个用于 API。当我从浏览器登录时,一切正常,授权通过并重定向到我的 API 工作正常。但是当我尝试通过 Postman 进行操作时,我无法访问 API。
请看这个链接,我从这个https://www.baeldung.com/sso-spring-security-oauth2复制了很多代码
技术栈:Java 8、Spring Boot、Spring Web、Spring Security、OAuth2。
我尝试使用不同的配置和许多选项,但到目前为止,我已将代码返回到传出状态,以便您可以告诉我可能是什么错误。
认证模块:
server:
port: 8081
servlet:
context-path: /auth
@SpringBootApplication
@EnableResourceServer
public class AuthApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(AuthApplication.class, args);
}
}
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my-client")
.secret(passwordEncoder.encode("secret"))
.authorizedGrantTypes("authorization_code", "client_credentials")
.scopes("user_info", "read", "write", "trust")
.autoApprove(true)
.accessTokenValiditySeconds(5000)
.redirectUris("http://localhost:8080/api/login");
}
}
@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers("/login", "/oauth/authorize")
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("john")
.password(passwordEncoder().encode("john"))
.roles("USER");
}
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
接口模块:
server:
servlet:
context-path: /api
security:
oauth2:
client:
clientId: my-client
clientSecret: secret
accessTokenUri: http://localhost:8081/auth/oauth/token
userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
resource:
userInfoUri: http://localhost:8081/auth/user/me
@Configuration
@EnableOAuth2Sso
@EnableWebSecurity
public class OAuthConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/login**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.logout().permitAll()
.and()
.httpBasic().disable();
}
}
@RestController
public class DashboardController {
@GetMapping("/demo")
public String demo() {
return "Hello";
}
}
解决方案
此问题的根本原因是 OAuth2 应用程序上的 @EnableResourceServer 注释。为什么?因为实际上 OAuth2 服务器不能同时是 Resource 服务和 Authentification 服务。所以我们需要简化OAuth2资源端的逻辑,去掉下面的注解。我要结束这个问题。
如果有人有问题 - 请写评论
推荐阅读
- java - 为什么我的 EC DSA 代码在验证时返回 false?
- android - Android模拟器主机文件不适用于Android应用
- python - Python中函数参数的数据类型
- flutter - 颤振弹出小部件
- html - 使用单选按钮更改 src
- stripe-payments - Stripe Webhook 事件 - 匹配客户 ID?
- android - 使用 startActivityForResult 方法启动时,singleTask 应用程序不会重用最近应用程序中已经存在的任务
- linux - linux shell脚本变量“未找到”
- python - 如何将所有请求发送到 CGI 脚本而不出现在 .Htaccess 文件的 URL 中?
- google-apps-script - 需要在 google 表格中创建一个应用脚本,该脚本根据另一个单元格值复制货币数据