使用java基于Spring Security 和OAuth2分别写一个用户鉴权、权限限制的demo
时间: 2024-05-19 15:10:22 浏览: 14
好的,下面分别给出基于 Spring Security 和 OAuth2 的用户鉴权、权限限制 Demo。
1. Spring Security Demo
首先,创建一个基于 Spring Boot 的 Maven 项目,并添加 Spring Security 依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
```
接着,在 `application.yml` 中配置用户信息:
```yml
spring:
security:
user:
name: admin
password: 123456
roles: ADMIN
```
其中,`name` 和 `password` 分别表示用户名和密码,`roles` 表示用户角色。
然后,创建一个 Spring Security 的配置类:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN") // ADMIN 角色才能访问 /admin/**
.anyRequest().authenticated() // 其他请求需要认证
.and().formLogin().permitAll(); // 开启表单登录
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication() // 在内存中配置用户信息
.withUser("admin").password("{noop}123456").roles("ADMIN"); // ADMIN 角色的用户
}
}
```
其中,`configure(HttpSecurity http)` 方法用于配置请求的安全策略,`configure(AuthenticationManagerBuilder auth)` 方法用于配置用户信息。在这里,我们将 ADMIN 角色的用户赋予访问 `/admin/**` 的权限。
最后,创建一个简单的控制器:
```java
@RestController
public class DemoController {
@GetMapping("/")
public String home() {
return "Hello, world!";
}
@GetMapping("/admin")
public String admin() {
return "Hello, admin!";
}
}
```
在这个控制器中,我们定义了两个请求,`/` 和 `/admin`,其中 `/` 是公共资源,任何人都可以访问,而 `/admin` 是受保护的资源,只有 ADMIN 角色的用户才能访问。
2. OAuth2 Demo
首先,创建一个基于 Spring Boot 的 Maven 项目,并添加 OAuth2 依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
</dependencies>
```
接着,在 `application.yml` 中配置 OAuth2 客户端信息:
```yml
spring:
security:
oauth2:
client:
registration:
google:
client-id: {your-client-id}
client-secret: {your-client-secret}
scope:
- email
- profile
```
其中,`client-id` 和 `client-secret` 分别表示 OAuth2 客户端的 ID 和密钥,`scope` 表示访问权限。
然后,创建一个 OAuth2 认证服务器:
```java
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory() // 在内存中配置客户端信息
.withClient("client") // 客户端 ID
.secret("{noop}secret") // 客户端密钥
.authorizedGrantTypes("authorization_code", "refresh_token") // 授权类型
.scopes("read", "write") // 访问权限
.redirectUris("http://localhost:8080/login/oauth2/code/google"); // 回调地址
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager); // 配置认证管理器
}
}
```
其中,`configure(ClientDetailsServiceConfigurer clients)` 方法用于配置客户端信息,`configure(AuthorizationServerEndpointsConfigurer endpoints)` 方法用于配置认证管理器。在这里,我们将使用 `inMemory` 方式配置客户端信息,并且支持 `authorization_code` 和 `refresh_token` 授权类型。
接着,创建一个 OAuth2 资源服务器:
```java
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**").authenticated() // 需要认证才能访问 /api/**
.anyRequest().permitAll(); // 其他请求允许访问
}
}
```
其中,`configure(HttpSecurity http)` 方法用于配置请求的安全策略。在这里,我们将 `/api/**` 定义为受保护的资源,只有认证后的用户才能访问。
最后,创建一个简单的控制器:
```java
@RestController
public class DemoController {
@GetMapping("/")
public String home() {
return "Hello, world!";
}
@GetMapping("/api")
public String api() {
return "Hello, OAuth2!";
}
}
```
在这个控制器中,我们定义了两个请求,`/` 和 `/api`,其中 `/` 是公共资源,任何人都可以访问,而 `/api` 是受保护的资源,只有认证后的用户才能访问。
以上就是基于 Spring Security 和 OAuth2 的用户鉴权、权限限制 Demo。希望对你有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)