使用 Spring Initializr 创建一个 Spring Cloud 项目,选择 Spring Boot 版本和其他依赖项。
在 pom.xml 文件中添加 Spring Cloud Security 的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
在 application.yml 文件中添加以下配置:
spring:
security:
user:
name: user
password: password
创建一个简单的 RESTful API,以便测试 Spring Cloud Security 的功能。例如,以下是一个返回 "Hello, World!" 的简单 API:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
在启动类上添加 @EnableOAuth2Sso
注解,以启用 Spring Cloud Security 的功能。
@SpringBootApplication
@EnableOAuth2Sso
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
运行应用程序,并使用浏览器或 Postman 等工具测试 /hello
API。您将被要求输入用户名和密码,输入之后可以成功访问 API。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有