<p>有些项目的接口是需要权限访问,比如限制IP、做权限控制,等等方案,本文是限制ip权限设置访问策略。</p>
<p>先看测试效果:</p>
<p style="margin-left:0cm;">测试:</p>
<p style="margin-left:0cm;">注意访问需要输入:<a data-token="62fb0901bdca31b1fd408c3f369c456b" href="http://127.0.0.1:8981/" rel="nofollow">http://127.0.0.1:8981/</a></p>
<p style="margin-left:0cm;">在配置文件加上本地的ip <span style="color:#2a00ff;">10.9.160.135</span></p>
<p style="margin-left:0cm;"><img alt="" class="blockcode" height="116" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-fad546b450b69a0ecc53a7dc9dd6ade2.png" width="336"></p>
<p style="margin-left:0cm;">去掉本地ip <span style="color:#2a00ff;">10.9.160.135 </span><span style="color:#2a00ff;">提示非法字符</span></p>
<p><img alt="" class="blockcode" height="209" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-44d586271caf4cc0e0aafd2ded39488b.png" width="734"></p>
<p>看一下项目结构,新建一个<strong><span style="color:#ff0000;">创建</span><span style="color:#ff0000;">IPLimitInterceptor类,在</span><span style="color:#ff0000;">springmvc配置文件配置、新建一个</span><span style="color:#ff0000;">ip</span><span style="color:#ff0000;">校验工具类 IPWhiteListUtil、ip配置文件</span><span style="color:#ff0000;">: ipwhite.properties。</span></strong></p>
<p><img alt="" class="blockcode" height="338" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-757eff46f565c152cf6629013e6ea51c.png" width="330"></p>
<p>再看代码实现</p>
<p style="margin-left:0cm;"><strong><span style="color:#ff0000;">创建</span></strong><strong><span style="color:#ff0000;">IPLimitInterceptor</span></strong><strong><span style="color:#ff0000;">类</span></strong></p>
<p style="margin-left:0cm;"><span style="color:#000000;">IPLimitInterceptor</span><span style="color:#000000;">继承</span><span style="color:#000000;">HandlerInterceptorAdapter</span><span style="color:#000000;">父类</span></p>
<p style="margin-left:0cm;">import java.net.InetAddress;</p>
<p style="margin-left:0cm;">import java.util.Properties;</p>
<p style="margin-left:0cm;">import javax.servlet.http.HttpServletRequest;</p>
<p style="margin-left:0cm;">import javax.servlet.http.HttpServletResponse;</p>
<p style="margin-left:0cm;">import org.springframework.core.io.support.PropertiesLoaderUtils;</p>
<p style="margin-left:0cm;">import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;</p>
<p style="margin-left:0cm;"></p>
<p style="margin-left:0cm;">public class IPLimitInterceptor extends HandlerInterceptorAdapter {</p>
<p style="margin-left:0cm;"></p>
<p style="margin-left:0cm;"> @Override</p>
<p style="margin-left:0cm;"> public boolean preHandle(HttpServletRequest request,</p>
<p style="margin-left:0cm;"> HttpServletResponse response, Object handler) throws Exception {</p>
<p style="margin-left:0cm;"> String ip = getIpAddress(request);</p>
<p style="margin-left:0cm;"> //读取ip白名单配置文件ipwhite.properties</p>
<p style="margin-left:0cm;"> Properties properties = PropertiesLoaderUtils.loadAllProperties("ipwhite.properties");</p>
<p style="margin-left:0cm;"> String ipWhilte = properties.getProperty("ipWhilte");</p>
<p style="margin-left:0cm;"> System.out.println(ipWhilte);</p>
<p style="margin-left:0cm;"> //判断请求ip地址 是否在白名单呢</p>
<p style="margin-left:0cm;"> if(IPWhiteListUtil.checkLoginIP(ip,ipWhilte)) {</p>
<p style="margin-left:0cm;"> return super.preHandle(request, response, handler);</p>
<p style="margin-left:0cm;"> }</p>
<p style="margin-left:0cm;"> throw new Exception("IP非法访问!");</p>
<p style="margin-left:0cm;"></p>
<p style="margin-left:0cm;"> }</p>
<p style="margin-left:0cm;"> //获取配置请求的ip地址</p>
<p style="margin-left:0cm;"> private String getIpAddress(HttpServletRequest request) {</p>
<p style="margin-left:0cm;"> String ip = request.getHeader("x-forwarded-for");</p>
<p style="margin-left:0cm;"> if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {</p>
<p style="margin-left:0cm;"> ip = request.getHeader("Proxy-Client-IP");</p>
<p style="margin-left:0cm;"> }</p>
<p style="margin-left:0cm;"> if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {</p>
<p style="margin-left:0cm;"> ip = request.getHeader("WL-Proxy-Client-IP");</p>
<p style="margin-left:0cm;"> }</p>
<p style="margin-left:0cm;"> if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {</p>
<p style="margin-left:0cm;"> ip = request.getHeader("HTTP_CLIENT_IP");</p>
<p style="mar |
|