parent
3b1f9f4d62
commit
cf08a71890
6 changed files with 144 additions and 8 deletions
@ -0,0 +1,50 @@ |
|||||||
|
package com.yipin.liuwanr.config; |
||||||
|
|
||||||
|
import com.yipin.liuwanr.filter.AuthInterceptor; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* @description |
||||||
|
* @author: Mr.JK |
||||||
|
* @create: 2021-06-03 17:26 |
||||||
|
**/ |
||||||
|
@Configuration |
||||||
|
@ConfigurationProperties(prefix = "auth") |
||||||
|
public class AuthConfig implements WebMvcConfigurer { |
||||||
|
|
||||||
|
//开启拦截字符串
|
||||||
|
public static String openInterceptStr; |
||||||
|
|
||||||
|
//放行url
|
||||||
|
public static List<String> permitUrl; |
||||||
|
|
||||||
|
@Value("${auth.openInterceptStr}") |
||||||
|
public void setOpenInterceptStr(String openInterceptStr) { |
||||||
|
AuthConfig.openInterceptStr = openInterceptStr; |
||||||
|
} |
||||||
|
|
||||||
|
@Value("${auth.permitUrl}") |
||||||
|
public void setPermitUrl(List<String> permitUrl) { |
||||||
|
AuthConfig.permitUrl = permitUrl; |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
public AuthInterceptor initAuthInterceptor(){ |
||||||
|
return new AuthInterceptor(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||||
|
registry.addInterceptor(initAuthInterceptor()).addPathPatterns(openInterceptStr) |
||||||
|
.excludePathPatterns(permitUrl); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
package com.yipin.liuwanr.filter; |
||||||
|
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate; |
||||||
|
import org.springframework.data.redis.core.ValueOperations; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
import org.springframework.web.servlet.HandlerInterceptor; |
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.util.Objects; |
||||||
|
|
||||||
|
/** |
||||||
|
* @description |
||||||
|
* @author: Mr.JK |
||||||
|
* @create: 2021-06-03 17:29 |
||||||
|
**/ |
||||||
|
public class AuthInterceptor implements HandlerInterceptor { |
||||||
|
|
||||||
|
@Resource |
||||||
|
StringRedisTemplate stringRedisTemplate; |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
||||||
|
response.setCharacterEncoding("UTF-8"); |
||||||
|
response.setContentType("text/html;charset=utf-8"); |
||||||
|
String token = request.getHeader("token"); |
||||||
|
if (StringUtils.isEmpty(token)) { |
||||||
|
//用户未登录,请登录后操作!
|
||||||
|
response.getWriter().print("0"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); |
||||||
|
Object loginStatus = ops.get(token); |
||||||
|
|
||||||
|
|
||||||
|
if( Objects.isNull(loginStatus)){ |
||||||
|
//token错误
|
||||||
|
response.getWriter().print("1"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue