├── doc ├── code.png ├── code1.png ├── logo.png └── SNAPSHOT.md ├── src └── main │ ├── resources │ └── META-INF │ │ └── spring.factories │ └── java │ └── com │ └── aizuda │ └── easy │ └── security │ ├── handler │ ├── FunctionHandler.java │ ├── RepFunctionHandler.java │ ├── ReqFunctionHandler.java │ ├── FunctionSharedVariables.java │ ├── AbstractFunctionHandler.java │ └── exec │ │ ├── BlacklistHandler.java │ │ ├── EncryptionPathHandler.java │ │ ├── ProjectPathHandler.java │ │ ├── AuthenticationHandler.java │ │ ├── ReqDataHandler.java │ │ ├── AuthorizationHandler.java │ │ └── DecryptPathHandler.java │ ├── exp │ ├── IErrorCode.java │ └── impl │ │ ├── AuthenticationException.java │ │ ├── AuthorizationException.java │ │ └── BasicException.java │ ├── server │ ├── EasySecurityServer.java │ ├── auth │ │ └── AuthorizeServer.java │ ├── EasySecurityServerImpl.java │ └── encryption │ │ └── CiphertextServer.java │ ├── HandlerFactory.java │ ├── annotation │ └── yapi │ │ └── YApiRule.java │ ├── code │ ├── FilterOrderCode.java │ └── BasicCode.java │ ├── domain │ ├── LocalEntity.java │ ├── Req.java │ └── Rep.java │ ├── util │ ├── PathCheckUtil.java │ ├── LocalUtil.java │ ├── IPUtil.java │ └── AesEncryptUtil.java │ ├── DefaultHandlerFactory.java │ ├── AbstractHandlerFactory.java │ ├── filter │ ├── wrapper │ │ ├── ReqWrapper.java │ │ └── RepWrapper.java │ └── FunctionFilter.java │ └── properties │ ├── SecurityProperties.java │ └── SecurityAutoConfiguration.java ├── readme.md ├── pom.xml └── LICENSE /doc/code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aizuda/easy-security/HEAD/doc/code.png -------------------------------------------------------------------------------- /doc/code1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aizuda/easy-security/HEAD/doc/code1.png -------------------------------------------------------------------------------- /doc/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aizuda/easy-security/HEAD/doc/logo.png -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.aizuda.easy.security.properties.SecurityAutoConfiguration 3 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/FunctionHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler; 2 | 3 | public interface FunctionHandler { 4 | 5 | Integer getIndex(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/exp/IErrorCode.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.exp; 2 | 3 | 4 | public interface IErrorCode { 5 | 6 | 7 | Integer getCode(); 8 | 9 | String getMsg(); 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/server/EasySecurityServer.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.server; 2 | 3 | import com.aizuda.easy.security.server.auth.AuthorizeServer; 4 | import com.aizuda.easy.security.server.encryption.CiphertextServer; 5 | 6 | public interface EasySecurityServer extends AuthorizeServer, CiphertextServer { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/server/auth/AuthorizeServer.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.server.auth; 2 | 3 | import com.aizuda.easy.security.exp.impl.BasicException; 4 | 5 | import java.util.List; 6 | 7 | public interface AuthorizeServer { 8 | 9 | Object getAuthUser(String token)throws BasicException; 10 | 11 | List getAuthorizeUrl(String token) throws BasicException; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/RepFunctionHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler; 2 | 3 | import com.aizuda.easy.security.exp.impl.BasicException; 4 | 5 | import javax.servlet.http.HttpServletResponse; 6 | import java.io.IOException; 7 | 8 | public interface RepFunctionHandler extends FunctionHandler{ 9 | 10 | String exec(HttpServletResponse response, String json) throws BasicException, IOException; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/HandlerFactory.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security; 2 | 3 | import com.aizuda.easy.security.handler.FunctionHandler; 4 | 5 | import java.util.Collection; 6 | 7 | public interface HandlerFactory { 8 | 9 | FunctionHandler getFunctionHandler(Integer index); 10 | 11 | Collection getFunctionHandlers(); 12 | 13 | boolean contain(Integer index, FunctionHandler functionHandler); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/ReqFunctionHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler; 2 | 3 | import com.aizuda.easy.security.exp.impl.BasicException; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import java.io.IOException; 7 | 8 | public interface ReqFunctionHandler extends FunctionHandler { 9 | 10 | String exec(HttpServletRequest request, String json) throws BasicException, IOException; 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/annotation/yapi/YApiRule.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.annotation.yapi; 2 | 3 | 4 | import java.lang.annotation.*; 5 | 6 | @Documented 7 | @Retention(RetentionPolicy.CLASS) 8 | @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE}) 9 | public @interface YApiRule { 10 | 11 | boolean required() default false; 12 | 13 | boolean hide() default false; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/FunctionSharedVariables.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler; 2 | 3 | import com.aizuda.easy.security.properties.SecurityProperties; 4 | import com.aizuda.easy.security.server.EasySecurityServer; 5 | 6 | public interface FunctionSharedVariables { 7 | 8 | void setEasySecurityServer(EasySecurityServer easySecurityServer); 9 | 10 | void setProperties(SecurityProperties properties); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/exp/impl/AuthenticationException.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.exp.impl; 2 | 3 | import com.aizuda.easy.security.code.BasicCode; 4 | import com.aizuda.easy.security.exp.IErrorCode; 5 | 6 | 7 | public class AuthenticationException extends BasicException { 8 | 9 | public AuthenticationException(String msg){ 10 | super(BasicCode.BASIC_CODE_401.getCode(),msg); 11 | } 12 | 13 | public AuthenticationException(IErrorCode iErrorCode){ 14 | super(iErrorCode); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/exp/impl/AuthorizationException.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.exp.impl; 2 | 3 | import com.aizuda.easy.security.code.BasicCode; 4 | import com.aizuda.easy.security.exp.IErrorCode; 5 | 6 | 7 | public class AuthorizationException extends BasicException { 8 | 9 | 10 | public AuthorizationException(String msg){ 11 | super(BasicCode.BASIC_CODE_403.getCode(),msg); 12 | } 13 | 14 | public AuthorizationException(IErrorCode iErrorCode){ 15 | super(iErrorCode); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/server/EasySecurityServerImpl.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.server; 2 | 3 | import com.aizuda.easy.security.exp.impl.BasicException; 4 | 5 | import java.util.List; 6 | 7 | public class EasySecurityServerImpl implements EasySecurityServer{ 8 | 9 | @Override 10 | public Object getAuthUser(String token) throws BasicException { 11 | return null; 12 | } 13 | 14 | @Override 15 | public List getAuthorizeUrl(String token) throws BasicException { 16 | return null; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # easy-security 2 | 3 | ![](doc/logo.png) 4 | 5 | > 官网查看详细教程: https://easy-security.aizuda.com/ 6 | 7 | easy-security 基于过滤器实现的一款配合spring快速开发的安全认证框架,思想是希望通过简单的配置,并且实现核心的方法达到认证和鉴权的目的。 8 | 9 | easy-security 不限制存取token方式,无论是保存到服务端还是使用JWT等都可以,因为这部分是由开发者自己来定义的,只需要告诉 easy-security 该如何获取用户信息即可。 10 | 11 | 如果你使用了 easy-security 自身所带的 Req 请求封装,那么所有的接口请求均以POST方式,Req 会把认证后的用户所携带在每次请求中,当需要获取用户的时候可以通过 Req 直接获取,解耦开发者获取认证用户的 12 | 13 | easy-security 结合了 Yapi 的使用,如果你使用 Yapi 需要在自己的项目中描述规则 14 | 15 | ### 功能列举 16 | * 认证拦截 17 | * 权限校验 18 | * 用户获取 19 | * 黑名单 20 | * 密文传输(内置AES加密算法) 21 | 22 | ### 加入社区 23 | ![](doc/code.png) 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/code/FilterOrderCode.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.code; 2 | 3 | 4 | 5 | public enum FilterOrderCode { 6 | 7 | /** 8 | * 过滤去顺序 9 | */ 10 | FILTER_ORDER_CODE_0(0, "核心过滤器加载"), 11 | ; 12 | 13 | 14 | 15 | private Integer code; 16 | 17 | private String name; 18 | 19 | FilterOrderCode(Integer code, String name) { 20 | this.code = code; 21 | this.name = name; 22 | } 23 | 24 | public Integer getCode() { 25 | return code; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | 33 | @Override 34 | public String toString() { 35 | return "{\"code\": "+getCode()+", \"msg\": \""+getName()+"\", \"data\": null }"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/domain/LocalEntity.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.domain; 2 | 3 | public class LocalEntity { 4 | 5 | private Object user; 6 | 7 | private Boolean special = false; 8 | 9 | private Boolean project = false; 10 | 11 | public Boolean getSpecial() { 12 | return special; 13 | } 14 | 15 | public void setSpecial(Boolean special) { 16 | this.special = special; 17 | } 18 | 19 | public Boolean getProject() { 20 | return project; 21 | } 22 | 23 | public void setProject(Boolean project) { 24 | this.project = project; 25 | } 26 | 27 | public Object getUser() { 28 | return user; 29 | } 30 | 31 | public void setUser(Object user) { 32 | this.user = user; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/domain/Req.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.domain; 2 | 3 | import com.aizuda.easy.security.annotation.yapi.YApiRule; 4 | 5 | public class Req { 6 | 7 | @YApiRule(required = true) 8 | private T data; 9 | 10 | @YApiRule(hide = true) 11 | private U user; 12 | 13 | @YApiRule(hide = true) 14 | private String token; 15 | 16 | public T getData() { 17 | return data; 18 | } 19 | 20 | public void setData(T data) { 21 | this.data = data; 22 | } 23 | 24 | public U getUser() { 25 | return user; 26 | } 27 | 28 | public void setUser(U user) { 29 | this.user = user; 30 | } 31 | 32 | public String getToken() { 33 | return token; 34 | } 35 | 36 | public void setToken(String token) { 37 | this.token = token; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/AbstractFunctionHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler; 2 | 3 | import com.aizuda.easy.security.properties.SecurityProperties; 4 | import com.aizuda.easy.security.server.EasySecurityServer; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | 7 | public abstract class AbstractFunctionHandler implements FunctionHandler,FunctionSharedVariables{ 8 | 9 | public EasySecurityServer easySecurityServer; 10 | 11 | public SecurityProperties properties; 12 | 13 | public ObjectMapper mapper = new ObjectMapper(); 14 | 15 | @Override 16 | public void setEasySecurityServer(EasySecurityServer easySecurityServer) { 17 | this.easySecurityServer = easySecurityServer; 18 | } 19 | 20 | @Override 21 | public void setProperties(SecurityProperties properties) { 22 | this.properties = properties; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/util/PathCheckUtil.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.util; 2 | 3 | import java.util.List; 4 | import java.util.function.Consumer; 5 | 6 | 7 | public class PathCheckUtil { 8 | 9 | private static final String RULE_0 = "**"; 10 | 11 | private static final String RULE_1 = "/"; 12 | 13 | public static void pathMatch(List configureUrl, String url, Consumer consumer) { 14 | boolean b = isMatch(configureUrl,url); 15 | if(b){ 16 | consumer.accept(b); 17 | } 18 | } 19 | 20 | public static Boolean isMatch(List configureUrl, String url){ 21 | return configureUrl.stream() 22 | .anyMatch(i -> { 23 | String ir = i.replace(RULE_0, ""); 24 | return i.equals(url) || (i.endsWith(RULE_0) && url.startsWith(ir)) || ir.equals(url + RULE_1); 25 | }); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/util/LocalUtil.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.util; 2 | 3 | import cn.hutool.core.util.ObjectUtil; 4 | import com.aizuda.easy.security.domain.LocalEntity; 5 | 6 | public class LocalUtil { 7 | 8 | private static ThreadLocal local = new ThreadLocal(); 9 | 10 | public static LocalEntity getLocalEntity(){ 11 | return local.get(); 12 | } 13 | 14 | public static void destroy(){ 15 | local.remove(); 16 | } 17 | 18 | public static LocalEntity create(){ 19 | LocalEntity localEntity = new LocalEntity(); 20 | local.set(localEntity); 21 | return localEntity; 22 | } 23 | 24 | public static T getUser(){ 25 | LocalEntity localEntity = getLocalEntity(); 26 | if(ObjectUtil.isNull(localEntity) || ObjectUtil.isNull(localEntity.getUser())){ 27 | return null; 28 | } 29 | return (T) localEntity.getUser(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/DefaultHandlerFactory.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security; 2 | 3 | import cn.hutool.core.util.ObjectUtil; 4 | import com.aizuda.easy.security.handler.FunctionHandler; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.ConcurrentHashMap; 8 | 9 | public class DefaultHandlerFactory extends AbstractHandlerFactory implements HandlerFactory { 10 | 11 | private Map functionHandlerMap = new ConcurrentHashMap<>(); 12 | 13 | public void register(Integer index, FunctionHandler functionHandler) { 14 | if(ObjectUtil.isNull(index) || ObjectUtil.isNull(functionHandler)){ 15 | return; 16 | } 17 | functionHandlerMap.put(index, functionHandler); 18 | } 19 | 20 | public void remove(Integer index) { 21 | functionHandlerMap.remove(index); 22 | } 23 | 24 | 25 | @Override 26 | public Map getFactoryMap() { 27 | return functionHandlerMap; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/exp/impl/BasicException.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.exp.impl; 2 | 3 | import com.aizuda.easy.security.exp.IErrorCode; 4 | 5 | import javax.servlet.ServletException; 6 | 7 | 8 | public class BasicException extends ServletException { 9 | 10 | private Integer code; 11 | private String msg; 12 | 13 | public Integer getCode() { 14 | return code; 15 | } 16 | 17 | public void setCode(Integer code) { 18 | this.code = code; 19 | } 20 | 21 | public String getMsg() { 22 | return msg; 23 | } 24 | 25 | public void setMsg(String msg) { 26 | this.msg = msg; 27 | } 28 | 29 | private BasicException(){ 30 | } 31 | 32 | public BasicException(Integer code,String msg){ 33 | super(msg); 34 | this.code = code; 35 | this.msg = msg; 36 | } 37 | 38 | public BasicException(IErrorCode iErrorCode){ 39 | super(iErrorCode.getMsg()); 40 | this.code = iErrorCode.getCode(); 41 | this.msg = iErrorCode.getMsg(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/AbstractHandlerFactory.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security; 2 | 3 | import com.aizuda.easy.security.handler.FunctionHandler; 4 | 5 | import java.util.Collection; 6 | import java.util.Map; 7 | import java.util.stream.Collectors; 8 | 9 | public abstract class AbstractHandlerFactory implements HandlerFactory{ 10 | 11 | @Override 12 | public FunctionHandler getFunctionHandler(Integer index) { 13 | return getFactoryMap().get(index); 14 | } 15 | 16 | @Override 17 | public Collection getFunctionHandlers() { 18 | Map factoryMap = getFactoryMap(); 19 | return factoryMap 20 | .keySet().stream() 21 | .sorted() 22 | .map(factoryMap::get) 23 | .collect(Collectors.toList()); 24 | } 25 | 26 | @Override 27 | public boolean contain(Integer index, FunctionHandler functionHandler) { 28 | return getFactoryMap().containsKey(index); 29 | } 30 | 31 | public abstract Map getFactoryMap(); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/BlacklistHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | import com.aizuda.easy.security.code.BasicCode; 4 | import com.aizuda.easy.security.exp.impl.BasicException; 5 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 6 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 7 | import com.aizuda.easy.security.util.IPUtil; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.stereotype.Component; 11 | 12 | import javax.servlet.http.HttpServletRequest; 13 | 14 | @Component 15 | public class BlacklistHandler extends AbstractFunctionHandler implements ReqFunctionHandler { 16 | private static final Logger log = LoggerFactory.getLogger(BlacklistHandler.class); 17 | @Override 18 | public String exec(HttpServletRequest request, String json) throws BasicException { 19 | String ip = IPUtil.getIp(request); 20 | log.debug("Accessing the user's IP: {}",ip); 21 | if(properties.getBlackList().contains(ip)){ 22 | throw new BasicException(BasicCode.BASIC_CODE_99984); 23 | } 24 | return json; 25 | } 26 | 27 | @Override 28 | public Integer getIndex() { 29 | return Integer.MIN_VALUE + 10; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/EncryptionPathHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | import cn.hutool.core.collection.CollectionUtil; 4 | import com.aizuda.easy.security.exp.impl.BasicException; 5 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 6 | import com.aizuda.easy.security.handler.RepFunctionHandler; 7 | import com.aizuda.easy.security.util.PathCheckUtil; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.IOException; 12 | import java.util.List; 13 | 14 | @Component 15 | public class EncryptionPathHandler extends AbstractFunctionHandler implements RepFunctionHandler { 16 | 17 | @Override 18 | public Integer getIndex() { 19 | return Integer.MAX_VALUE - 10; 20 | } 21 | 22 | @Override 23 | public String exec(HttpServletResponse response, String json) throws BasicException, IOException { 24 | List urlFilter = properties.getDecryptUrl(); 25 | String url = response.getHeader("requestUri"); 26 | if(CollectionUtil.isEmpty(urlFilter) || !PathCheckUtil.isMatch(urlFilter,url)){ 27 | return json; 28 | } 29 | return easySecurityServer.encryption(response, json, properties.getSecretKey()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/ProjectPathHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | import cn.hutool.core.collection.CollectionUtil; 4 | import com.aizuda.easy.security.domain.LocalEntity; 5 | import com.aizuda.easy.security.exp.impl.BasicException; 6 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 7 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 8 | import com.aizuda.easy.security.util.LocalUtil; 9 | import com.aizuda.easy.security.util.PathCheckUtil; 10 | import org.springframework.stereotype.Component; 11 | 12 | import javax.servlet.http.HttpServletRequest; 13 | import java.util.List; 14 | 15 | @Component 16 | public class ProjectPathHandler extends AbstractFunctionHandler implements ReqFunctionHandler { 17 | 18 | @Override 19 | public String exec(HttpServletRequest request, String json) throws BasicException { 20 | LocalEntity localEntity = LocalUtil.getLocalEntity();; 21 | List urlFilter = properties.getProjectUrl(); 22 | if(!CollectionUtil.isEmpty(urlFilter)){ 23 | String url = request.getRequestURI(); 24 | PathCheckUtil.pathMatch(urlFilter,url,localEntity::setProject); 25 | } 26 | return json; 27 | } 28 | 29 | @Override 30 | public Integer getIndex() { 31 | return Integer.MIN_VALUE + 20; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/server/encryption/CiphertextServer.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.server.encryption; 2 | 3 | import cn.hutool.core.util.StrUtil; 4 | import com.aizuda.easy.security.code.BasicCode; 5 | import com.aizuda.easy.security.exp.impl.BasicException; 6 | import com.aizuda.easy.security.util.AesEncryptUtil; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.util.UUID; 11 | 12 | public interface CiphertextServer { 13 | 14 | String IV = "iv"; 15 | 16 | default String encryption(HttpServletResponse response, String json, String key) throws BasicException { 17 | String iv = getIvValue(); 18 | response.setHeader(IV, iv); 19 | json = AesEncryptUtil.encryption(json, key, iv); 20 | if(StrUtil.isEmpty(json)){ 21 | throw new BasicException(BasicCode.BASIC_CODE_99988); 22 | } 23 | return json.trim(); 24 | } 25 | 26 | default String decryption(HttpServletRequest request, String json,String key) throws BasicException { 27 | if(StrUtil.isEmpty(key)){ 28 | throw new BasicException(BasicCode.BASIC_CODE_99990); 29 | } 30 | String iv = request.getHeader(IV); 31 | if(StrUtil.isEmpty(iv)){ 32 | throw new BasicException(BasicCode.BASIC_CODE_99989); 33 | } 34 | json = AesEncryptUtil.decryption(json, key,iv); 35 | if(StrUtil.isEmpty(json)){ 36 | throw new BasicException(BasicCode.BASIC_CODE_99987); 37 | } 38 | return json; 39 | } 40 | 41 | default String getIvValue(){ 42 | return UUID.randomUUID().toString().replace("-","").substring(16); 43 | } 44 | 45 | default String getIV(){ 46 | return IV; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/AuthenticationHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | 4 | import cn.hutool.core.util.ObjectUtil; 5 | import cn.hutool.core.util.StrUtil; 6 | import com.aizuda.easy.security.code.BasicCode; 7 | import com.aizuda.easy.security.domain.LocalEntity; 8 | import com.aizuda.easy.security.exp.impl.AuthenticationException; 9 | import com.aizuda.easy.security.exp.impl.BasicException; 10 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 11 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 12 | import com.aizuda.easy.security.util.LocalUtil; 13 | import org.springframework.stereotype.Component; 14 | 15 | import javax.servlet.http.HttpServletRequest; 16 | import java.io.IOException; 17 | 18 | @Component 19 | public class AuthenticationHandler extends AbstractFunctionHandler implements ReqFunctionHandler { 20 | 21 | @Override 22 | public String exec(HttpServletRequest request, String json) throws BasicException, IOException { 23 | // 不为特殊路径和项目路径才获取用户信息 24 | LocalEntity localEntity = LocalUtil.getLocalEntity(); 25 | if(localEntity.getSpecial() || localEntity.getProject() || !properties.getAuthEnable()){ 26 | return json; 27 | } 28 | String token = request.getHeader(properties.getTokenKey()); 29 | if (StrUtil.isEmpty(token)) { 30 | throw new AuthenticationException(BasicCode.BASIC_CODE_401); 31 | } 32 | Object obj = easySecurityServer.getAuthUser(token); 33 | if(ObjectUtil.isEmpty(obj)){ 34 | throw new AuthenticationException(BasicCode.BASIC_CODE_401); 35 | } 36 | localEntity.setUser(obj); 37 | return json; 38 | } 39 | 40 | @Override 41 | public Integer getIndex() { 42 | return Integer.MIN_VALUE + 30; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/ReqDataHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | import cn.hutool.core.util.ObjectUtil; 4 | import cn.hutool.core.util.StrUtil; 5 | import com.aizuda.easy.security.code.BasicCode; 6 | import com.aizuda.easy.security.domain.LocalEntity; 7 | import com.aizuda.easy.security.domain.Req; 8 | import com.aizuda.easy.security.exp.impl.BasicException; 9 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 10 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 11 | import com.aizuda.easy.security.util.LocalUtil; 12 | import org.springframework.stereotype.Component; 13 | 14 | import javax.servlet.http.HttpServletRequest; 15 | import java.io.IOException; 16 | import java.util.Locale; 17 | 18 | @Component 19 | public class ReqDataHandler extends AbstractFunctionHandler implements ReqFunctionHandler { 20 | 21 | @Override 22 | public String exec(HttpServletRequest request, String json) throws BasicException, IOException { 23 | String method = request.getMethod().toUpperCase(Locale.ROOT); 24 | LocalEntity localEntity = LocalUtil.getLocalEntity(); 25 | if(StrUtil.isEmpty(json) || !properties.getRequestDataEnable() || localEntity.getSpecial()) { 26 | return json; 27 | } 28 | if(!method.equals("POST")){ 29 | throw new BasicException(BasicCode.BASIC_CODE_99997); 30 | } 31 | Req req = mapper.readValue(json, Req.class); 32 | if (ObjectUtil.isEmpty(req)) { 33 | req = new Req<>(); 34 | } 35 | String token = request.getHeader(properties.getTokenKey()); 36 | req.setToken(token); 37 | req.setUser(localEntity.getUser()); 38 | return mapper.writeValueAsString(req); 39 | } 40 | 41 | @Override 42 | public Integer getIndex() { 43 | return Integer.MIN_VALUE + 60; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/util/IPUtil.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.util; 2 | 3 | import cn.hutool.core.util.StrUtil; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import java.net.InetAddress; 7 | import java.net.UnknownHostException; 8 | 9 | public class IPUtil { 10 | 11 | private static final String IP = "127.0.0.1"; 12 | 13 | private static final String UNKNOWN = "unknown"; 14 | 15 | /** 16 | * 如果使用nginx需要配置X-Real-IP,配置方式如下: 17 | * location / { proxy_set_header X-Real-IP $remote_addr; } 18 | * @param request 19 | * @return 20 | */ 21 | public static String getIp(HttpServletRequest request) { 22 | String ip = request.getHeader("X-Forwarded-For"); 23 | if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)|| IP.equalsIgnoreCase(ip)) { 24 | ip = request.getHeader("Proxy-Client-IP"); 25 | } 26 | if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)|| IP.equalsIgnoreCase(ip)) { 27 | ip = request.getHeader("WL-Proxy-Client-IP"); 28 | } 29 | if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)|| IP.equalsIgnoreCase(ip)) { 30 | ip = request.getHeader("X-Real-IP"); 31 | } 32 | if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)|| IP.equalsIgnoreCase(ip)) { 33 | ip = request.getRemoteAddr(); 34 | } 35 | if (StrUtil.isBlank(ip) || IP.equals(ip)|| ip.indexOf(":") > -1) { 36 | try { 37 | ip = InetAddress.getLocalHost().getHostAddress(); 38 | } catch (UnknownHostException e) { 39 | ip = null; 40 | } 41 | } 42 | // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 43 | if (ip != null && ip.length() > 15) { 44 | if (ip.indexOf(",") > 0) { 45 | ip = ip.substring(0, ip.indexOf(",")); 46 | } 47 | } 48 | return ip; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/code/BasicCode.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.code; 2 | 3 | import cn.hutool.http.HttpStatus; 4 | import com.aizuda.easy.security.exp.IErrorCode; 5 | 6 | 7 | public enum BasicCode implements IErrorCode { 8 | 9 | /** 10 | * 校验码 11 | */ 12 | BASIC_CODE_401(HttpStatus.HTTP_UNAUTHORIZED, "未登录"), 13 | BASIC_CODE_402(HttpStatus.HTTP_PAYMENT_REQUIRED, "数据未找到"), 14 | BASIC_CODE_403(HttpStatus.HTTP_FORBIDDEN,"无操作权限"), 15 | BASIC_CODE_404(HttpStatus.HTTP_NOT_FOUND,"不存在的接口"), 16 | BASIC_CODE_409(HttpStatus.HTTP_CONFLICT,"数据已存在"), 17 | BASIC_CODE_500(HttpStatus.HTTP_INTERNAL_ERROR,"系统内部错误"), 18 | 19 | BASIC_CODE_99984(-99984,"您已被列为黑名单"), 20 | BASIC_CODE_99985(-99985,"登录会话过期"), 21 | BASIC_CODE_99986(-99986,"密钥验证错误"), 22 | BASIC_CODE_99987(-99987,"数据解密失败"), 23 | BASIC_CODE_99988(-99988,"数据加密失败"), 24 | BASIC_CODE_99989(-99989,"公钥(IV)不能为空"), 25 | BASIC_CODE_99990(-99990,"私钥(KEY)不能为空"), 26 | BASIC_CODE_99991(-99991,"特殊路径解析异常"), 27 | BASIC_CODE_99992(-99992,"项目路径解析异常"), 28 | BASIC_CODE_99993(-99993,"data数据解析异常"), 29 | BASIC_CODE_99994(-99994,"数据解密异常"), 30 | BASIC_CODE_99995(-99995,"验证码不正确或已失效,请刷新验证码"), 31 | BASIC_CODE_99996(-99996,"参数校验失败"), 32 | BASIC_CODE_99997(-99997,"不支持的请求方式"), 33 | BASIC_CODE_99998(-99998,"参数列表不匹配,或传参错误"), 34 | BASIC_CODE_99999(-99999,"参数不能为空"), 35 | ; 36 | 37 | 38 | 39 | private Integer code; 40 | 41 | private String msg; 42 | 43 | BasicCode(Integer code, String msg) { 44 | this.code = code; 45 | this.msg = msg; 46 | } 47 | 48 | @Override 49 | public Integer getCode() { 50 | return code; 51 | } 52 | 53 | @Override 54 | public String getMsg() { 55 | return msg; 56 | } 57 | 58 | 59 | @Override 60 | public String toString() { 61 | return "{\"code\": "+getCode()+", \"msg\": \""+getMsg()+"\", \"data\": null }"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/domain/Rep.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.domain; 2 | 3 | import cn.hutool.http.HttpStatus; 4 | import com.aizuda.easy.security.exp.IErrorCode; 5 | 6 | 7 | public class Rep{ 8 | 9 | private Integer code; 10 | private String msg; 11 | private T data; 12 | 13 | public Integer getCode() { 14 | return code; 15 | } 16 | 17 | public void setCode(Integer code) { 18 | this.code = code; 19 | } 20 | 21 | public String getMsg() { 22 | return msg; 23 | } 24 | 25 | public void setMsg(String msg) { 26 | this.msg = msg; 27 | } 28 | 29 | public T getData() { 30 | return data; 31 | } 32 | 33 | public void setData(T data) { 34 | this.data = data; 35 | } 36 | 37 | protected Rep(Integer code, String msg, T data){ 38 | this.code = code; 39 | this.msg = msg; 40 | this.data = data; 41 | } 42 | 43 | public static Rep ok(){ 44 | return new Rep(HttpStatus.HTTP_OK,null,null); 45 | } 46 | 47 | public static Rep ok(T data){ 48 | return new Rep(HttpStatus.HTTP_OK,null,data); 49 | } 50 | 51 | public static Rep ok(IErrorCode iErrorCode){ 52 | return new Rep(iErrorCode.getCode(),iErrorCode.getMsg(),null); 53 | } 54 | 55 | public static Rep ok(IErrorCode iErrorCode, T obj){ 56 | return new Rep(iErrorCode.getCode(),iErrorCode.getMsg(),obj); 57 | } 58 | 59 | public static Rep error(IErrorCode iErrorCode){ 60 | return new Rep(iErrorCode.getCode(),iErrorCode.getMsg(),null); 61 | } 62 | 63 | public static Rep error(Rep rep){ 64 | return new Rep(rep.getCode(), rep.getMsg(),null); 65 | } 66 | 67 | public static Rep error(Integer code, String msg){ 68 | return new Rep(code,msg,null); 69 | } 70 | 71 | public Rep() { 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/AuthorizationHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | import cn.hutool.core.collection.CollectionUtil; 4 | import cn.hutool.core.util.StrUtil; 5 | import com.aizuda.easy.security.code.BasicCode; 6 | import com.aizuda.easy.security.domain.LocalEntity; 7 | import com.aizuda.easy.security.exp.impl.AuthenticationException; 8 | import com.aizuda.easy.security.exp.impl.AuthorizationException; 9 | import com.aizuda.easy.security.exp.impl.BasicException; 10 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 11 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 12 | import com.aizuda.easy.security.util.LocalUtil; 13 | import org.springframework.stereotype.Component; 14 | 15 | import javax.servlet.http.HttpServletRequest; 16 | import java.util.List; 17 | 18 | @Component 19 | public class AuthorizationHandler extends AbstractFunctionHandler implements ReqFunctionHandler { 20 | 21 | @Override 22 | public String exec(HttpServletRequest request,String json) throws BasicException { 23 | LocalEntity localEntity = LocalUtil.getLocalEntity(); 24 | if(localEntity.getSpecial() || localEntity.getProject() || !properties.getAuthorizeEnable()){ 25 | return json; 26 | } 27 | String token = request.getHeader(properties.getTokenKey()); 28 | if (StrUtil.isEmpty(token)) { 29 | throw new AuthenticationException(BasicCode.BASIC_CODE_401); 30 | } 31 | List list = easySecurityServer.getAuthorizeUrl(token); 32 | if(CollectionUtil.isEmpty(list)){ 33 | throw new AuthorizationException(BasicCode.BASIC_CODE_403); 34 | } 35 | String url = request.getRequestURI(); 36 | if(!list.contains(url)){ 37 | throw new AuthorizationException(BasicCode.BASIC_CODE_403); 38 | } 39 | return json; 40 | } 41 | 42 | @Override 43 | public Integer getIndex() { 44 | return Integer.MIN_VALUE + 40; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/handler/exec/DecryptPathHandler.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.handler.exec; 2 | 3 | import cn.hutool.core.collection.CollectionUtil; 4 | import cn.hutool.json.JSONUtil; 5 | import com.aizuda.easy.security.exp.impl.BasicException; 6 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 7 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 8 | import com.aizuda.easy.security.util.PathCheckUtil; 9 | import com.fasterxml.jackson.core.JsonProcessingException; 10 | import com.fasterxml.jackson.databind.JavaType; 11 | import org.springframework.stereotype.Component; 12 | 13 | import javax.servlet.http.HttpServletRequest; 14 | import java.io.IOException; 15 | import java.util.ArrayList; 16 | import java.util.LinkedHashMap; 17 | import java.util.List; 18 | 19 | @Component 20 | public class DecryptPathHandler extends AbstractFunctionHandler implements ReqFunctionHandler { 21 | 22 | @Override 23 | public String exec(HttpServletRequest request, String json) throws BasicException, IOException { 24 | List urlFilter = properties.getDecryptUrl(); 25 | String url = request.getRequestURI(); 26 | if(CollectionUtil.isEmpty(urlFilter) || !PathCheckUtil.isMatch(urlFilter,url)){ 27 | return json; 28 | } 29 | return easySecurityServer.decryption(request,json, properties.getSecretKey()); 30 | } 31 | 32 | @Override 33 | public Integer getIndex() { 34 | return Integer.MIN_VALUE + 50; 35 | } 36 | 37 | 38 | private Object jsonToObject(String json) throws JsonProcessingException { 39 | if(!JSONUtil.isTypeJSON(json)){ 40 | return json; 41 | } 42 | if(JSONUtil.isTypeJSONObject(json)){ 43 | return mapper.readValue(json, LinkedHashMap.class); 44 | } 45 | if(JSONUtil.isTypeJSONArray(json)){ 46 | JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Object.class); 47 | mapper.readValue(json,javaType); 48 | } 49 | return json; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/util/AesEncryptUtil.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.util; 2 | 3 | import org.apache.commons.codec.Charsets; 4 | import org.apache.commons.codec.binary.Base64; 5 | 6 | import javax.crypto.Cipher; 7 | import javax.crypto.spec.IvParameterSpec; 8 | import javax.crypto.spec.SecretKeySpec; 9 | 10 | public class AesEncryptUtil { 11 | 12 | private static final String INSTANCE = "AES/CBC/NoPadding"; 13 | 14 | private static final String ALGORITHM = "AES"; 15 | 16 | public static String encryption(String data, String key, String iv){ 17 | try { 18 | //"算法/模式/补码方式"NoPadding PkcsPadding 19 | Cipher cipher = Cipher.getInstance(INSTANCE); 20 | int blockSize = cipher.getBlockSize(); 21 | byte[] dataBytes = data.getBytes(); 22 | int plaintextLength = dataBytes.length; 23 | if (plaintextLength % blockSize != 0) { 24 | plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); 25 | } 26 | byte[] plaintext = new byte[plaintextLength]; 27 | System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); 28 | SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); 29 | IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes()); 30 | cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); 31 | byte[] encrypted = cipher.doFinal(plaintext); 32 | return new Base64().encodeToString(encrypted); 33 | } catch (Exception e) { 34 | e.printStackTrace(); 35 | return null; 36 | } 37 | } 38 | 39 | 40 | public static String decryption(String data, String key, String iv) { 41 | try { 42 | byte[] encrypted1 = new Base64().decode(data); 43 | Cipher cipher = Cipher.getInstance(INSTANCE); 44 | SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); 45 | IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes()); 46 | cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); 47 | byte[] original = cipher.doFinal(encrypted1); 48 | return new String(original, Charsets.UTF_8); 49 | } catch (Exception e) { 50 | e.printStackTrace(); 51 | return null; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/filter/wrapper/ReqWrapper.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.filter.wrapper; 2 | 3 | import com.aizuda.easy.security.HandlerFactory; 4 | import com.aizuda.easy.security.exp.impl.BasicException; 5 | import com.aizuda.easy.security.handler.FunctionHandler; 6 | import com.aizuda.easy.security.handler.ReqFunctionHandler; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | import javax.servlet.ReadListener; 11 | import javax.servlet.ServletInputStream; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletRequestWrapper; 14 | import java.io.*; 15 | import java.nio.charset.StandardCharsets; 16 | 17 | 18 | public class ReqWrapper extends HttpServletRequestWrapper { 19 | 20 | private static final Logger log = LoggerFactory.getLogger(ReqWrapper.class); 21 | private String body; 22 | 23 | public ReqWrapper(HttpServletRequest request,HandlerFactory factory) throws IOException, BasicException { 24 | super(request); 25 | body = getBodyContent(request); 26 | for (FunctionHandler functionHandler : factory.getFunctionHandlers()) { 27 | if(functionHandler instanceof ReqFunctionHandler) { 28 | log.debug("exec handler : {}",functionHandler.getClass().getName()); 29 | body = ((ReqFunctionHandler)functionHandler).exec(request,body); 30 | } 31 | } 32 | } 33 | 34 | @Override 35 | public BufferedReader getReader() throws IOException { 36 | return new BufferedReader(new InputStreamReader(getInputStream())); 37 | } 38 | 39 | @Override 40 | public ServletInputStream getInputStream() throws IOException { 41 | final ByteArrayInputStream bais = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)); 42 | return new ServletInputStream() { 43 | @Override 44 | public int read() throws IOException { 45 | return bais.read(); 46 | } 47 | @Override 48 | public boolean isFinished() { 49 | return false; 50 | } 51 | @Override 52 | public boolean isReady() { 53 | return false; 54 | } 55 | @Override 56 | public void setReadListener(ReadListener readListener) { 57 | } 58 | }; 59 | } 60 | 61 | private String getBodyContent(HttpServletRequest request) throws IOException { 62 | StringBuilder sb = new StringBuilder(); 63 | InputStream inputStream = request.getInputStream(); 64 | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); 65 | String line; 66 | while ((line = reader.readLine()) != null) { 67 | sb.append(line); 68 | } 69 | return sb.toString(); 70 | } 71 | 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/filter/FunctionFilter.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.filter; 2 | 3 | import com.aizuda.easy.security.HandlerFactory; 4 | import com.aizuda.easy.security.domain.LocalEntity; 5 | import com.aizuda.easy.security.exp.impl.BasicException; 6 | import com.aizuda.easy.security.filter.wrapper.RepWrapper; 7 | import com.aizuda.easy.security.filter.wrapper.ReqWrapper; 8 | import com.aizuda.easy.security.properties.SecurityProperties; 9 | import com.aizuda.easy.security.util.LocalUtil; 10 | import com.aizuda.easy.security.util.PathCheckUtil; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import javax.servlet.*; 15 | import javax.servlet.http.HttpServletRequest; 16 | import javax.servlet.http.HttpServletResponse; 17 | import java.io.IOException; 18 | import java.nio.charset.StandardCharsets; 19 | import java.util.List; 20 | 21 | 22 | public class FunctionFilter implements Filter { 23 | 24 | private static final Logger log = LoggerFactory.getLogger(FunctionFilter.class); 25 | private static final String PARAMES = "?code=%s&msg=%s"; 26 | private SecurityProperties properties; 27 | private HandlerFactory factory; 28 | public FunctionFilter(SecurityProperties properties,HandlerFactory factory) { 29 | this.properties = properties; 30 | this.factory = factory; 31 | } 32 | 33 | @Override 34 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 35 | HttpServletRequest request = (HttpServletRequest) servletRequest; 36 | HttpServletResponse response = (HttpServletResponse) servletResponse; 37 | try { 38 | LocalUtil.create(); 39 | specialPathHandler(request); 40 | request.setCharacterEncoding(StandardCharsets.UTF_8.name()); 41 | response.setCharacterEncoding(StandardCharsets.UTF_8.name()); 42 | if(!LocalUtil.getLocalEntity().getSpecial()){ 43 | request = new ReqWrapper(request,factory); 44 | response = new RepWrapper(response,factory); 45 | } 46 | filterChain.doFilter(request, response); 47 | if(response instanceof RepWrapper) { 48 | response.setHeader("requestUri", request.getRequestURI()); 49 | ((RepWrapper) response).changeContent(); 50 | } 51 | } catch (BasicException e) { 52 | log.error(e.getMsg()); 53 | forward(request,response, properties.getErrorUrl(),e); 54 | }finally { 55 | LocalUtil.destroy(); 56 | } 57 | } 58 | 59 | public void forward(HttpServletRequest request, HttpServletResponse response, String errUrl, BasicException basicException) throws ServletException, IOException { 60 | String url = errUrl + String.format(PARAMES, basicException.getCode(),basicException.getMsg()); 61 | request.getRequestDispatcher(url).forward(request,response); 62 | } 63 | 64 | private void specialPathHandler (HttpServletRequest request){ 65 | LocalEntity localEntity = LocalUtil.getLocalEntity(); 66 | List urlFilter = properties.getSpecialUrl(); 67 | String url = request.getRequestURI(); 68 | // 确定有该URL 就需要放到线程变量中 69 | PathCheckUtil.pathMatch(urlFilter,url,(bol) -> localEntity.setSpecial(bol)); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/properties/SecurityProperties.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.properties; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | 9 | @ConfigurationProperties(prefix = "easy.security") 10 | public class SecurityProperties { 11 | 12 | private Boolean authEnable = false; 13 | 14 | private Boolean authorizeEnable = false; 15 | 16 | private Boolean requestDataEnable = false; 17 | 18 | /** 19 | * request.header(key) 20 | */ 21 | private String tokenKey = "token"; 22 | /** 23 | * 项目路径 24 | */ 25 | private List projectUrl = new ArrayList<>(); 26 | /** 27 | * 特殊路径 28 | */ 29 | private List specialUrl = new ArrayList<>(); 30 | /** 31 | * 解密路径 32 | */ 33 | private List decryptUrl = new ArrayList<>(); 34 | /** 35 | * 黑名单 36 | */ 37 | private List blackList = new ArrayList<>(); 38 | /** 39 | * 发生异常跳转地址 40 | */ 41 | private String errorUrl = "/failure/authenticationFilter"; 42 | /** 43 | * 长度16位的字母数字组合 44 | */ 45 | private String secretKey; 46 | 47 | public boolean getAuthEnable() { 48 | return authEnable; 49 | } 50 | 51 | public void setAuthEnable(boolean authEnable) { 52 | this.authEnable = authEnable; 53 | } 54 | 55 | public boolean getAuthorizeEnable() { 56 | return authorizeEnable; 57 | } 58 | 59 | public void setAuthorizeEnable(Boolean authorizeEnable) { 60 | this.authorizeEnable = authorizeEnable; 61 | } 62 | 63 | public String getTokenKey() { 64 | return tokenKey; 65 | } 66 | 67 | public void setTokenKey(String tokenKey) { 68 | this.tokenKey = tokenKey; 69 | } 70 | 71 | public List getProjectUrl() { 72 | return projectUrl; 73 | } 74 | 75 | public void setProjectUrl(List projectUrl) { 76 | this.projectUrl = projectUrl; 77 | } 78 | 79 | public List getSpecialUrl() { 80 | return specialUrl; 81 | } 82 | 83 | public void setSpecialUrl(List specialUrl) { 84 | this.specialUrl = specialUrl; 85 | } 86 | 87 | public List getDecryptUrl() { 88 | return decryptUrl; 89 | } 90 | 91 | public void setDecryptUrl(List decryptUrl) { 92 | this.decryptUrl = decryptUrl; 93 | } 94 | 95 | public String getErrorUrl() { 96 | return errorUrl; 97 | } 98 | 99 | public void setErrorUrl(String errorUrl) { 100 | this.errorUrl = errorUrl; 101 | } 102 | 103 | public Boolean getRequestDataEnable() { 104 | return requestDataEnable; 105 | } 106 | 107 | public void setRequestDataEnable(Boolean requestDataEnable) { 108 | this.requestDataEnable = requestDataEnable; 109 | } 110 | 111 | public String getSecretKey() { 112 | return secretKey; 113 | } 114 | 115 | public void setSecretKey(String secretKey) { 116 | this.secretKey = secretKey; 117 | } 118 | 119 | public List getBlackList() { 120 | return blackList; 121 | } 122 | 123 | public void setBlackList(List blackList) { 124 | this.blackList = blackList; 125 | } 126 | 127 | 128 | 129 | } 130 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/filter/wrapper/RepWrapper.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.filter.wrapper; 2 | 3 | import cn.hutool.core.util.ObjectUtil; 4 | import com.aizuda.easy.security.HandlerFactory; 5 | import com.aizuda.easy.security.exp.impl.BasicException; 6 | import com.aizuda.easy.security.handler.FunctionHandler; 7 | import com.aizuda.easy.security.handler.RepFunctionHandler; 8 | import org.apache.commons.codec.Charsets; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import javax.servlet.ServletOutputStream; 13 | import javax.servlet.WriteListener; 14 | import javax.servlet.http.HttpServletResponse; 15 | import javax.servlet.http.HttpServletResponseWrapper; 16 | import java.io.ByteArrayOutputStream; 17 | import java.io.IOException; 18 | import java.io.OutputStreamWriter; 19 | import java.io.PrintWriter; 20 | 21 | public class RepWrapper extends HttpServletResponseWrapper { 22 | 23 | private static final Logger log = LoggerFactory.getLogger(RepWrapper.class); 24 | private ByteArrayOutputStream buffer; 25 | private ServletOutputStream out; 26 | private PrintWriter writer; 27 | private HttpServletResponse response; 28 | private String body; 29 | private HandlerFactory factory; 30 | public RepWrapper(HttpServletResponse response,HandlerFactory factory){ 31 | super(response); 32 | this.response = response; 33 | this.factory = factory; 34 | //真正存储数据的流 35 | buffer = new ByteArrayOutputStream(); 36 | out = new WapperedOutputStream(buffer); 37 | writer = new PrintWriter(new OutputStreamWriter(buffer, Charsets.UTF_8)); 38 | } 39 | 40 | @Override 41 | public ServletOutputStream getOutputStream(){ 42 | return out; 43 | } 44 | 45 | @Override 46 | public PrintWriter getWriter(){ 47 | return writer; 48 | } 49 | 50 | @Override 51 | public void flushBuffer()throws IOException{ 52 | if(out!=null){ 53 | out.flush(); 54 | } 55 | if(writer!=null){ 56 | writer.flush(); 57 | } 58 | } 59 | 60 | @Override 61 | public void reset(){ 62 | buffer.reset(); 63 | } 64 | 65 | public void changeContent() throws IOException, BasicException { 66 | PrintWriter printWriter = null; 67 | try { 68 | flushBuffer(); 69 | body = buffer.toString(); 70 | printWriter = response.getWriter(); 71 | for (FunctionHandler functionHandler : factory.getFunctionHandlers()) { 72 | if(functionHandler instanceof RepFunctionHandler) { 73 | log.debug("exec handler : {}", functionHandler.getClass().getName()); 74 | body = ((RepFunctionHandler)functionHandler).exec(response,body); 75 | } 76 | } 77 | }finally { 78 | if(!ObjectUtil.isNull(printWriter)){ 79 | printWriter.write(body); 80 | printWriter.flush(); 81 | printWriter.close(); 82 | } 83 | } 84 | } 85 | 86 | private class WapperedOutputStream extends ServletOutputStream{ 87 | private ByteArrayOutputStream bos; 88 | public WapperedOutputStream(ByteArrayOutputStream stream){ 89 | bos=stream; 90 | } 91 | @Override 92 | public void write(int b){ 93 | bos.write(b); 94 | } 95 | @Override 96 | public boolean isReady() { 97 | return false; 98 | } 99 | @Override 100 | public void setWriteListener(WriteListener writeListener) { 101 | } 102 | } 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/aizuda/easy/security/properties/SecurityAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.aizuda.easy.security.properties; 2 | 3 | 4 | import com.aizuda.easy.security.DefaultHandlerFactory; 5 | import com.aizuda.easy.security.HandlerFactory; 6 | import com.aizuda.easy.security.code.FilterOrderCode; 7 | import com.aizuda.easy.security.filter.FunctionFilter; 8 | import com.aizuda.easy.security.handler.AbstractFunctionHandler; 9 | import com.aizuda.easy.security.handler.FunctionHandler; 10 | import com.aizuda.easy.security.server.EasySecurityServer; 11 | import com.aizuda.easy.security.server.EasySecurityServerImpl; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.beans.BeansException; 15 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 16 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 17 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 18 | import org.springframework.boot.web.servlet.FilterRegistrationBean; 19 | import org.springframework.context.ApplicationContext; 20 | import org.springframework.context.ApplicationContextAware; 21 | import org.springframework.context.annotation.Bean; 22 | import org.springframework.context.annotation.ComponentScan; 23 | import org.springframework.context.annotation.Configuration; 24 | 25 | import java.util.Map; 26 | 27 | @Configuration 28 | @ConditionalOnClass(value = {SecurityProperties.class,EasySecurityServer.class}) 29 | @EnableConfigurationProperties(SecurityProperties.class) 30 | @ComponentScan(value = {"com.aizuda.easy.security"}) 31 | public class SecurityAutoConfiguration extends DefaultHandlerFactory implements HandlerFactory, ApplicationContextAware { 32 | 33 | private static final Logger log = LoggerFactory.getLogger(SecurityAutoConfiguration.class); 34 | 35 | private final String urlPatterns = "/*"; 36 | 37 | private ApplicationContext context; 38 | 39 | final SecurityProperties securityProperties; 40 | 41 | public SecurityAutoConfiguration(SecurityProperties securityProperties) { 42 | this.securityProperties = securityProperties; 43 | } 44 | 45 | @ConditionalOnMissingBean(EasySecurityServer.class) 46 | @Bean 47 | public EasySecurityServer easySecurityServer(){ 48 | return new EasySecurityServerImpl(); 49 | } 50 | 51 | @Bean 52 | public FilterRegistrationBean functionFilter(EasySecurityServer easySecurityServer) { 53 | log.info("building {}",FilterOrderCode.FILTER_ORDER_CODE_0.getName()); 54 | init(easySecurityServer); 55 | FilterRegistrationBean registration = new FilterRegistrationBean<>(); 56 | FunctionFilter functionFilter = new FunctionFilter(securityProperties,this); 57 | registration.setFilter(functionFilter); 58 | registration.addUrlPatterns(urlPatterns); 59 | registration.setName("functionFilter"); 60 | registration.setOrder(FilterOrderCode.FILTER_ORDER_CODE_0.getCode()); 61 | return registration; 62 | } 63 | 64 | @Override 65 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 66 | this.context = applicationContext; 67 | } 68 | 69 | private void init(EasySecurityServer easySecurityServer){ 70 | Map beansOfType = context.getBeansOfType(FunctionHandler.class); 71 | beansOfType.values().forEach(item -> { 72 | if(item instanceof AbstractFunctionHandler){ 73 | AbstractFunctionHandler abstractFunctionHandler = (AbstractFunctionHandler) item; 74 | abstractFunctionHandler.setProperties(securityProperties); 75 | abstractFunctionHandler.setEasySecurityServer(easySecurityServer); 76 | } 77 | register(item.getIndex(), item); 78 | }); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /doc/SNAPSHOT.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.aizuda 8 | easy-security-boot-starter 9 | 2.0.4 10 | 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 2.7.12 16 | 17 | 18 | 19 | 20 | 1.8 21 | 1.8 22 | 1.8 23 | UTF-8 24 | 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | true 32 | 33 | 34 | org.yaml 35 | snakeyaml 36 | 37 | 38 | 39 | 40 | 41 | org.yaml 42 | snakeyaml 43 | 2.0 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-configuration-processor 49 | true 50 | 51 | 52 | 53 | javax.servlet 54 | javax.servlet-api 55 | 4.0.1 56 | 57 | 58 | 59 | com.fasterxml.jackson.core 60 | jackson-databind 61 | 2.14.1 62 | 63 | 64 | 65 | commons-codec 66 | commons-codec 67 | 1.13 68 | 69 | 70 | 71 | cn.hutool 72 | hutool-all 73 | 5.8.20 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | easy-security 82 | easy-security 83 | https://github.com/aizuda/easy-security 84 | 85 | 86 | 87 | 88 | The Apache Software License, Version 2.0 89 | http://www.apache.org/licenses/LICENSE-2.0.txt 90 | 91 | 92 | 93 | 94 | 95 | 96 | bigUncle 97 | bigUncle 98 | 875730567@qq.com 99 | 100 | Project Manager 101 | Architect 102 | 103 | +8 104 | 105 | 106 | 107 | 108 | 109 | https://github.com/aizuda/easy-security.git 110 | https://easy-security.aizuda.com/ 111 | https://easy-security.aizuda.com/ 112 | 113 | 114 | 115 | 116 | 117 | release 118 | 119 | true 120 | 121 | 122 | 123 | 124 | 125 | org.apache.maven.plugins 126 | maven-release-plugin 127 | 2.5.3 128 | 129 | true 130 | false 131 | release 132 | deploy 133 | 134 | 135 | 136 | 137 | 138 | org.apache.maven.plugins 139 | maven-source-plugin 140 | 2.2.1 141 | 142 | 143 | attach-sources 144 | 145 | jar-no-fork 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | org.apache.maven.plugins 154 | maven-javadoc-plugin 155 | 2.9.1 156 | 157 | 158 | attach-javadocs 159 | 160 | jar 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | org.apache.maven.plugins 169 | maven-gpg-plugin 170 | 1.5 171 | 172 | 173 | sign-artifacts 174 | verify 175 | 176 | sign 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | release 189 | https://s01.oss.sonatype.org/content/repositories/snapshots 190 | 191 | 192 | release 193 | https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------