tlias管理系统-过滤器Filter入门程序
This commit is contained in:
@@ -2,7 +2,9 @@ package com.inmind;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||||
|
|
||||||
|
@ServletComponentScan//开启对servlet组件的支持
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class TliasWebManagementApplication {
|
public class TliasWebManagementApplication {
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,16 @@ package com.inmind.controller;
|
|||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import com.inmind.pojo.Result;
|
import com.inmind.pojo.Result;
|
||||||
import com.inmind.service.EmpService;
|
import com.inmind.service.EmpService;
|
||||||
|
import com.inmind.utils.JwtUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
@@ -23,6 +27,17 @@ public class LoginController {
|
|||||||
log.info("登录操作:{}",emp);
|
log.info("登录操作:{}",emp);
|
||||||
//调用员工业务层查询功能
|
//调用员工业务层查询功能
|
||||||
Emp e = empService.getEmpByUserNameAndPassWord(emp);
|
Emp e = empService.getEmpByUserNameAndPassWord(emp);
|
||||||
return e != null?Result.success():Result.error("用户名或密码错误");
|
//登录成功,生成令牌,下发令牌
|
||||||
|
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 lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.servlet.*;
|
||||||
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
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方法,过滤器的销毁方法执行了");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.utils;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class JwtUtils {
|
||||||
|
|
||||||
|
private static String signKey = "inmind";
|
||||||
|
private static Long expriation = 60 * 60 * 1000L;
|
||||||
|
|
||||||
|
//生成令牌
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param claims 自定义载荷数据(json数据)
|
||||||
|
* @return 令牌字符串
|
||||||
|
*/
|
||||||
|
public static String generateJwt(Map<String, Object> claims){
|
||||||
|
String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, signKey)
|
||||||
|
.setClaims(claims)//设置载荷(自定义数据,用户信息数据)
|
||||||
|
.setExpiration(new Date(System.currentTimeMillis()+expriation))//设置过期时间(有效时间为1小时)
|
||||||
|
.compact();
|
||||||
|
return jwt;
|
||||||
|
}
|
||||||
|
|
||||||
|
//解析令牌
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param jwt 要校验的令牌字符串
|
||||||
|
* @return 解析之后的载荷数据
|
||||||
|
*/
|
||||||
|
public static Claims parseJWT(String jwt){
|
||||||
|
Claims claims = Jwts.parser()
|
||||||
|
.setSigningKey(signKey)//设置签名秘钥(解析JWT时,最重要的就是输入正确的签名秘钥)
|
||||||
|
.parseClaimsJws(jwt)
|
||||||
|
.getBody();
|
||||||
|
return claims;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user