javaEEday11-tlias管理系统-过滤器Filter入门程序
This commit is contained in:
@@ -2,7 +2,9 @@ package com.inmind;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
|
||||
@ServletComponentScan//开启对servlet组件的支持
|
||||
@SpringBootApplication
|
||||
public class TliasWebApplication {
|
||||
|
||||
|
||||
@@ -3,12 +3,16 @@ package com.inmind.controller;
|
||||
import com.inmind.pojo.Emp;
|
||||
import com.inmind.pojo.Result;
|
||||
import com.inmind.service.EmpService;
|
||||
import com.inmind.utils.JwtUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class LoginController {
|
||||
@@ -21,6 +25,17 @@ public class LoginController {
|
||||
log.info("登录操作:{}",emp);
|
||||
//调用员工的业务层,根据用户名与密码查询员工是否存在,表示登录是否成功
|
||||
Emp e = empService.login(emp);
|
||||
return e ==null?Result.error("用户名或密码错误"):Result.success();
|
||||
//登录成功,生成令牌,下发令牌
|
||||
if (e != null) {
|
||||
//生成令牌
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put("id", e.getId());
|
||||
claims.put("name", e.getName());
|
||||
claims.put("username", e.getUsername());
|
||||
String jwt = JwtUtils.generateJwt(claims);
|
||||
return Result.success(jwt);
|
||||
}
|
||||
|
||||
return Result.error("用户名或密码错误");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.inmind.filter;
|
||||
|
||||
import jakarta.servlet.*;
|
||||
import jakarta.servlet.annotation.WebFilter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@WebFilter(urlPatterns = "/*")//标识为过滤器组件,拦截所有请求
|
||||
public class DemoFilter implements Filter {
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
Filter.super.init(filterConfig);
|
||||
log.info("init方法,过滤器的初始化方法执行了");
|
||||
}
|
||||
|
||||
//拦截到请求之后,调用多次
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
log.info("doFilter方法,过滤器的拦截方法执行了");
|
||||
//放行API
|
||||
filterChain.doFilter(servletRequest,servletResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
Filter.super.destroy();
|
||||
log.info("destroy方法,过滤器的销毁方法执行了");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user