我想知道如何使用提供的功能在 Spring Boot 3-legged 身份验证上检索访问令牌 org.springframework.boot:spring-boot-starter-oauth2-client
我能够使用常规RestTemplate调用获取访问令牌。
我尝试spring-boot-starter-oauth2-client按照https://github.com/wonwoo/spring-boot-oauth2-login 中的示例使用这些功能获取相同的访问令牌。
我能够检索服务器提供的代码,但我不知道如何获取访问令牌。
我的代码如下所示:
属性在application.properties:
spring.security.oauth2.client.registration.my-client-name-here.client-id=__client_id_here__
spring.security.oauth2.client.registration.my-client-name-here.client-secret=__client_secret_here__
spring.security.oauth2.client.registration.my-client-name-here.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.my-client-name-here.redirect-uri-template=http://localhost:8080/authentication/3leggedtoken/callback
spring.security.oauth2.client.registration.my-client-name-here.scope=data:read
spring.security.oauth2.client.registration.my-client-name-here.client-name=__client_name_here__
spring.security.oauth2.client.registration.my-client-name-here.client-authentication-method=POST
spring.security.oauth2.client.provider.my-client-name-here.token-uri=https://example.com/api/token
spring.security.oauth2.client.provider.my-client-name-here.authorization-uri=https://example.com/api/authorize
spring.security.oauth2.client.provider.my-client-name-here.user-info-uri=
spring.security.oauth2.client.provider.my-client-name-here.user-name-attribute=
Run Code Online (Sandbox Code Playgroud)
Thymeleaf 模板在login.html:
spring.security.oauth2.client.registration.my-client-name-here.client-id=__client_id_here__
spring.security.oauth2.client.registration.my-client-name-here.client-secret=__client_secret_here__
spring.security.oauth2.client.registration.my-client-name-here.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.my-client-name-here.redirect-uri-template=http://localhost:8080/authentication/3leggedtoken/callback
spring.security.oauth2.client.registration.my-client-name-here.scope=data:read
spring.security.oauth2.client.registration.my-client-name-here.client-name=__client_name_here__
spring.security.oauth2.client.registration.my-client-name-here.client-authentication-method=POST
spring.security.oauth2.client.provider.my-client-name-here.token-uri=https://example.com/api/token
spring.security.oauth2.client.provider.my-client-name-here.authorization-uri=https://example.com/api/authorize
spring.security.oauth2.client.provider.my-client-name-here.user-info-uri=
spring.security.oauth2.client.provider.my-client-name-here.user-name-attribute=
Run Code Online (Sandbox Code Playgroud)
配置SecurityConfig.java:
<div th:each="registration: ${registrations}">
<a th:href="@{${registration.uri}}">
Sign in with [[${registration.clientName}]]
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
控制器AuthenticationController.java:
@Configuration
@EnableWebSecurity
public class SegurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http_security) throws Exception {
http_security.authorizeRequests().requestMatchers(PathRequest.toStaticResources().atCommonLocations()) …Run Code Online (Sandbox Code Playgroud) 根据教程测试 Web 层,可以使用以下代码测试控制器是否已创建:
@Test
public void contexLoads() throws Exception {
assertThat(controller).isNotNull();
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
The method assertThat(T, Matcher<? super T>) in the type Assert is not applicable for the arguments (HomeController)"
Run Code Online (Sandbox Code Playgroud)
即使声明:
import static org.junit.Assert.assertThat;
Run Code Online (Sandbox Code Playgroud)
我的类的代码与示例中给出的代码相同:
package com.my_org.my_app;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SmokeTest {
@Autowired
private HomeController controller;
@Test
public void contexLoads() throws Exception {
assertThat(controller).isNotNull();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将断言语句更改为:
@Test
public void contexLoads() throws Exception { …Run Code Online (Sandbox Code Playgroud) 我想在 Windows 10 系统上安装 Apache 2 服务器。官方文档(https://httpd.apache.org/docs/2.4/platform/windows.html)指出:
“Apache HTTP 服务器项目本身不提供软件的二进制版本,仅提供源代码”
要获得 Apache HTTP Server 的二进制文件,必须自行编译或从第三方提供的发行版获取。这些发行版通常将 Apache 与 PHP 和 MySQL 捆绑在一起。
问题是:
建议使用什么发行版来安装独立的 Apache 2 服务器以及执行此操作的步骤是什么?
spring-boot ×2
access-token ×1
apache ×1
controller ×1
installation ×1
oauth-2.0 ×1
unit-testing ×1
windows ×1