给我写一个基于springboot 前后端分离 使用过滤器实现用户登录的代码
时间: 2024-02-27 14:53:32 浏览: 40
好的,下面是一个基于Spring Boot前后端分离,使用过滤器实现用户登录的代码示例:
后端代码:
1. 定义一个过滤器,用于验证用户是否登录,如果未登录则拦截请求并返回未登录提示信息。
```java
@Component
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 判断用户是否已经登录
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("user") == null) {
// 如果未登录,则返回未登录提示信息
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{\"code\":401,\"msg\":\"未登录\"}");
return;
}
// 如果已登录,则放行请求
filterChain.doFilter(request, response);
}
}
```
2. 在Spring Boot的配置类中配置过滤器,指定过滤器拦截的路径。
```java
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private LoginFilter loginFilter;
// 添加过滤器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginFilter)
.addPathPatterns("/api/**") // 拦截所有/api/**的请求
.excludePathPatterns("/api/user/login"); // 除了登录请求,其他请求都需要登录才能访问
}
}
```
3. 在Controller中添加登录接口,用于验证用户登录,并将用户信息存入Session中。
```java
@RestController
@RequestMapping("/api/user")
public class UserController {
@PostMapping("/login")
public Result login(@RequestBody User user, HttpSession session) {
// 验证用户账号密码是否正确,这里省略具体实现
if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
// 如果账号密码正确,则将用户信息存入Session
session.setAttribute("user", user);
return Result.success();
} else {
return Result.error("账号或密码错误");
}
}
}
```
前端代码:
1. 在登录页面中,获取用户输入的账号密码,并发送登录请求。
```html
<form>
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="button" class="btn btn-primary" onclick="login()">登录</button>
</form>
```
```javascript
function login() {
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url: "/api/user/login",
type: "POST",
contentType: "application/json;charset=UTF-8",
data: JSON.stringify({username: username, password: password}),
success: function (result) {
if (result.code == 200) {
// 登录成功
window.location.href = "/index.html";
} else {
// 登录失败,显示错误信息
alert(result.msg);
}
}
});
}
```
2. 在其他页面中,需要判断用户是否已登录,如果未登录则跳转到登录页面。
```javascript
$(function () {
$.ajax({
url: "/api/user",
type: "GET",
success: function (result) {
if (result.code == 401) {
// 用户未登录,跳转到登录页面
window.location.href = "/login.html";
}
}
});
});
```
以上就是一个基于Spring Boot前后端分离,使用过滤器实现用户登录的代码示例,希望能对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)