Apache Shiro 是一个强大且易用的 Java 安全框架,用于身份验证、授权、加密和会话管理。在 Spring Boot 应用程序中,结合 Shiro 进行日志管理可以通过以下步骤实现:
首先,在你的 pom.xml
文件中添加 Shiro 和 Spring Boot 相关依赖:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Apache Shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>1.8.0</version>
</dependency>
<!-- Logback for logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
创建一个配置类来设置 Shiro 的相关配置:
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
@Bean
public SecurityManager securityManager() {
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
return factory.getInstance();
}
}
创建一个 shiro.ini
文件来配置 Shiro 的用户、角色和权限:
[users]
admin=password, admin
user=password, user
[roles]
admin=user, admin
user=user
[urls]
/admin/** = authc, roles[admin]
/user/** = authc, roles[user]
创建一个 logback.xml
文件来配置日志记录:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache.shiro" level="DEBUG"/>
<logger name="org.springframework.web" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
在你的控制器中使用 Shiro 进行身份验证和授权:
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ShiroController {
@GetMapping("/login")
public String login(@RequestParam String username, @RequestParam String password) {
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
currentUser.login(token);
return "Login successful";
} catch (Exception e) {
return "Login failed: " + e.getMessage();
}
}
@GetMapping("/admin")
public String admin() {
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isPermitted("admin")) {
return "Welcome, Admin!";
} else {
return "Access denied";
}
}
@GetMapping("/user")
public String user() {
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isPermitted("user")) {
return "Welcome, User!";
} else {
return "Access denied";
}
}
}
启动你的 Spring Boot 应用程序,并访问以下 URL 进行测试:
http://localhost:8080/login?username=admin&password=password
http://localhost:8080/admin
http://localhost:8080/user
通过以上步骤,你可以在 Spring Boot 应用程序中结合 Shiro 进行日志管理,并使用 Shiro 进行身份验证和授权。