shiro如何实现登录安全认证,具体代码。
时间: 2024-02-18 09:03:43 浏览: 22
以下是使用 Shiro 实现登录安全认证的示例代码:
1. 添加 Shiro 依赖
在项目的 pom.xml 文件中添加 Shiro 的依赖:
```xml
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.7.1</version>
</dependency>
```
2. 配置 Shiro
在应用程序中配置 Shiro,包括安全认证的过滤器、realm、密码加密等。这里使用 INI 配置文件进行配置。
shiro.ini 文件内容如下:
```ini
[main]
# 配置自定义 Realm
myRealm = com.example.MyRealm
securityManager.realm = $myRealm
# 配置密码加密方式
credentialsMatcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName = MD5
credentialsMatcher.hashIterations = 1
myRealm.credentialsMatcher = $credentialsMatcher
[users]
# 配置用户信息
admin = 123456, admin
[urls]
# 配置受保护的 URL 路径
/login = anon
/logout = logout
/** = authc
```
3. 实现自定义 Realm
实现自定义 Realm 来实现用户认证信息的获取和验证。这里使用 IniRealm 实现。
MyRealm.java 文件内容如下:
```java
import org.apache.shiro.realm.text.IniRealm;
public class MyRealm extends IniRealm {
public MyRealm() {
super("classpath:shiro.ini");
}
}
```
4. 实现登录功能
在登录页面中获取用户输入的用户名和密码,然后将其传递给 Shiro 进行认证。这里使用 UsernamePasswordToken 来封装用户信息。
LoginController.java 文件内容如下:
```java
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(String username, String password, Model model) {
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
currentUser.login(token);
return "redirect:/index";
} catch (AuthenticationException e) {
model.addAttribute("error", "用户名或密码错误");
return "login";
}
}
}
```
5. 配置登录页面
在 Shiro 的过滤器中配置登录页面的访问路径,当用户未登录时自动跳转到登录页面。这里使用 Thymeleaf 模板引擎实现。
login.html 文件内容如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<div th:if="${error}">
<p th:text="${error}"></p>
</div>
<form method="post" action="/login">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<br>
<button type="submit">登录</button>
</form>
</body>
</html>
```
6. 实现注销功能
在应用程序中实现用户注销功能,同时清空用户的会话信息。这里使用 Shiro 提供的 Subject.logout() 方法实现。
LogoutController.java 文件内容如下:
```java
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LogoutController {
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout() {
SecurityUtils.getSubject().logout();
return "redirect:/login";
}
}
```
通过以上步骤,就可以使用 Shiro 实现登录安全认证功能。具体实现的细节可以参考 Shiro 的官方文档或者相关的教程。