【发布时间】:2019-02-24 02:21:30
【问题描述】:
我有一个相当基本的 Spring Boot 设置,我已经安装了 Spring Security,并且我成功地设置了 OAuth2 来保护我的 API。
几天前我遇到了一些麻烦,asked (and answered) a question 在达到我的/oauth/token 终点时遇到了麻烦。我很快发现问题在于我试图在我的POST 请求的正文中发送我的客户端凭据,但是在 Spring Security 中配置了令牌端点以接受客户端凭据(client_id 和 secret)而是通过 HTTP 基本身份验证。
我使用 OAuth2 API 的大部分经验都涉及在 POST 请求的正文中发送客户端凭据,我想知道是否可以将 Spring Security 配置为以相同的方式运行?
我尝试了一些不同的方法但没有成功,比如设置以下配置选项,但我觉得这可能只在配置 OAuth2 客户端时使用:
security.oauth2.client.clientAuthenticationScheme=form
这是我的授权服务器配置。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
import org.springframework.security.oauth2.provider.token.TokenStore;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client_id")
.secret("secret")
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.scopes("read", "write")
.accessTokenValiditySeconds(600)
.refreshTokenValiditySeconds(3600);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(this.tokenStore)
.userApprovalHandler(this.userApprovalHandler)
.authenticationManager(this.authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()")
.passwordEncoder(this.passwordEncoder);
}
}
【问题讨论】:
-
查看
allowFormAuthenticationForClients。 -
@chrylis 我也是,我不明白为什么。不过,我没有假设或直接问你这个问题,以防你觉得我这样做了。如果您想发布答案,我会很乐意接受。如果没有,我会自己发布一个,并为未来的访问者接受那个。
-
你自己写吧;我懒得解释清楚。 ;-)
标签: java spring spring-boot spring-security oauth-2.0