Skip to content

Commit 3426e1a

Browse files
author
孙健
committed
add rpc filters
1 parent 70e557c commit 3426e1a

File tree

13 files changed

+246
-288
lines changed

13 files changed

+246
-288
lines changed

src/main/java/org/nlpcn/jcoder/filter/HostFilter.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import org.nlpcn.commons.lang.util.StringUtil;
66
import org.nlpcn.jcoder.run.mvc.view.JsonView;
7+
import org.nlpcn.jcoder.server.rpc.RpcFilter;
8+
import org.nlpcn.jcoder.server.rpc.Rpcs;
9+
import org.nlpcn.jcoder.server.rpc.domain.RpcRequest;
710
import org.nlpcn.jcoder.util.ApiException;
811
import org.nlpcn.jcoder.util.Restful;
912
import org.nlpcn.jcoder.util.StaticValue;
@@ -13,7 +16,7 @@
1316
import org.slf4j.Logger;
1417
import org.slf4j.LoggerFactory;
1518

16-
public class HostFilter implements ActionFilter {
19+
public class HostFilter implements ActionFilter, RpcFilter {
1720

1821
private static final Logger LOG = LoggerFactory.getLogger(IpErrorCountFilter.class);
1922

@@ -36,4 +39,19 @@ public View match(ActionContext actionContext) {
3639
return null;
3740
}
3841

42+
@Override
43+
public Restful match(RpcRequest req) {
44+
45+
if (StringUtil.isBlank(host) || "*".equals(host)) {
46+
return null;
47+
}
48+
49+
if (!host.equals(Rpcs.getContext().localAddress())) {
50+
LOG.info(Rpcs.getContext().remoteAddress() + " only vist by host! the server name is : " + Rpcs.getContext().localAddress());
51+
return Restful.instance(false, req.getClassName() + "/" + req.getMethodName() + " only vist by host! ", null, ApiException.Unauthorized);
52+
}
53+
54+
return null;
55+
}
56+
3957
}

src/main/java/org/nlpcn/jcoder/filter/IpErrorCountFilter.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import org.nlpcn.commons.lang.util.StringUtil;
88
import org.nlpcn.jcoder.run.mvc.view.JsonView;
9+
import org.nlpcn.jcoder.server.rpc.RpcFilter;
10+
import org.nlpcn.jcoder.server.rpc.Rpcs;
11+
import org.nlpcn.jcoder.server.rpc.domain.RpcRequest;
912
import org.nlpcn.jcoder.util.ApiException;
1013
import org.nlpcn.jcoder.util.Restful;
1114
import org.nlpcn.jcoder.util.StaticValue;
@@ -25,7 +28,7 @@
2528
* @author ansj
2629
*
2730
*/
28-
public class IpErrorCountFilter implements ActionFilter {
31+
public class IpErrorCountFilter implements ActionFilter, RpcFilter {
2932

3033
private static final Logger LOG = LoggerFactory.getLogger(IpErrorCountFilter.class);
3134

@@ -47,14 +50,14 @@ public View match(ActionContext actionContext) {
4750
String ip = StaticValue.getRemoteHost(actionContext.getRequest());
4851

4952
AtomicInteger times = IP_CACHW.getIfPresent(ip);
50-
51-
if(times==null){
53+
54+
if (times == null) {
5255
return null;
5356
}
5457

5558
if (times.get() >= maxCount) {
5659
LOG.info(ip + "err times gather than " + maxCount);
57-
return new JsonView(Restful.instance(false, "your err times gather than " + maxCount + " please wait 20 minute try again",null,ApiException.Unauthorized));
60+
return new JsonView(Restful.instance(false, "your err times gather than " + maxCount + " please wait 20 minute try again", null, ApiException.Unauthorized));
5861
}
5962

6063
return null;
@@ -70,14 +73,33 @@ public static int err(String key) {
7073
AtomicInteger count = IP_CACHW.get(key, () -> {
7174
return new AtomicInteger();
7275
});
73-
int err = count.incrementAndGet() ;
76+
int err = count.incrementAndGet();
7477
LOG.info(key + " err times " + err);
75-
return err ;
78+
return err;
7679
} catch (ExecutionException e) {
7780
e.printStackTrace();
78-
LOG.error(e.getMessage(),e);
81+
LOG.error(e.getMessage(), e);
82+
}
83+
return 0;
84+
}
85+
86+
@Override
87+
public Restful match(RpcRequest req) {
88+
89+
String ip = Rpcs.getContext().remoteAddress();
90+
91+
AtomicInteger times = IP_CACHW.getIfPresent(ip);
92+
93+
if (times == null) {
94+
return null;
95+
}
96+
97+
if (times.get() >= maxCount) {
98+
LOG.info(ip + "err times gather than " + maxCount);
99+
return Restful.instance(false, "your err times gather than " + maxCount + " please wait 20 minute try again", null, ApiException.Unauthorized);
79100
}
80-
return 0 ;
101+
102+
return null;
81103
}
82104

83105
}

src/main/java/org/nlpcn/jcoder/filter/TokenFilter.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.nlpcn.commons.lang.util.StringUtil;
66
import org.nlpcn.jcoder.domain.Token;
77
import org.nlpcn.jcoder.run.mvc.view.JsonView;
8+
import org.nlpcn.jcoder.server.rpc.RpcFilter;
9+
import org.nlpcn.jcoder.server.rpc.Rpcs;
10+
import org.nlpcn.jcoder.server.rpc.domain.RpcRequest;
811
import org.nlpcn.jcoder.service.TokenService;
912
import org.nlpcn.jcoder.util.ApiException;
1013
import org.nlpcn.jcoder.util.Restful;
@@ -15,9 +18,9 @@
1518
import org.slf4j.Logger;
1619
import org.slf4j.LoggerFactory;
1720

18-
public class TokenFilter implements ActionFilter {
19-
20-
private static final Logger LOG = LoggerFactory.getLogger(TokenFilter.class) ;
21+
public class TokenFilter implements ActionFilter, RpcFilter {
22+
23+
private static final Logger LOG = LoggerFactory.getLogger(TokenFilter.class);
2124

2225
@Override
2326
public View match(ActionContext actionContext) {
@@ -29,15 +32,39 @@ public View match(ActionContext actionContext) {
2932
}
3033

3134
try {
32-
Token token2 = TokenService.getToken(token) ;
33-
if (token2==null || token2 == Token.NULL) {
35+
Token token2 = TokenService.getToken(token);
36+
if (token2 == null || token2 == Token.NULL) {
3437
LOG.info(StaticValue.getRemoteHost(actionContext.getRequest()) + " token not access");
3538
return new JsonView(Restful.instance(false, "token not access", null, ApiException.TokenAuthorNotFound));
3639
}
3740
} catch (ExecutionException e) {
3841
e.printStackTrace();
39-
LOG.error(e.getMessage(),e);
40-
return new JsonView(Restful.instance(false, e.getMessage(),e,ApiException.ServerException));
42+
LOG.error(e.getMessage(), e);
43+
return new JsonView(Restful.instance(false, e.getMessage(), e, ApiException.ServerException));
44+
}
45+
46+
return null;
47+
}
48+
49+
@Override
50+
public Restful match(RpcRequest req) {
51+
String token = req.getTokenStr();
52+
53+
if (StringUtil.isBlank(token)) {
54+
LOG.info(Rpcs.getContext().remoteAddress() + " token 'authorization' not in header");
55+
return Restful.instance(false, "token 'authorization' not in header ", null, ApiException.Unauthorized);
56+
}
57+
58+
try {
59+
Token token2 = TokenService.getToken(token);
60+
if (token2 == null || token2 == Token.NULL) {
61+
LOG.info(Rpcs.getContext().remoteAddress() + " token not access");
62+
return Restful.instance(false, "token not access", null, ApiException.TokenAuthorNotFound);
63+
}
64+
} catch (ExecutionException e) {
65+
e.printStackTrace();
66+
LOG.error(e.getMessage(), e);
67+
return Restful.instance(false, e.getMessage(), e, ApiException.ServerException);
4168
}
4269

4370
return null;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.nlpcn.jcoder.server.rpc;
2+
3+
import java.util.Map;
4+
5+
import org.nlpcn.jcoder.server.rpc.domain.RpcRequest;
6+
import org.nlpcn.jcoder.util.Restful;
7+
8+
public interface RpcFilter {
9+
10+
Restful match(RpcRequest req);
11+
}

src/main/java/org/nlpcn/jcoder/server/rpc/client/RpcDecoder.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/main/java/org/nlpcn/jcoder/server/rpc/domain/RpcContext.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.nlpcn.jcoder.server.rpc.domain;
22

3+
import java.net.InetSocketAddress;
34
import java.util.HashMap;
45
import java.util.Map;
56

7+
import org.nlpcn.jcoder.server.rpc.Rpcs;
8+
69
import io.netty.channel.ChannelHandlerContext;
710

811
public class RpcContext {
@@ -16,8 +19,8 @@ public class RpcContext {
1619
private RpcResponse rep;
1720

1821
private Map<Object, Object> map = null;
19-
20-
private int type ;
22+
23+
private int type;
2124

2225
public RpcContext(ChannelHandlerContext ctx) {
2326
this.chContext = ctx;
@@ -69,6 +72,12 @@ public void setType(int type) {
6972
this.type = type;
7073
}
7174

75+
public String remoteAddress() {
76+
return ((InetSocketAddress) getChContext().channel().remoteAddress()).getAddress().getHostAddress() ;
77+
}
7278

73-
79+
public String localAddress() {
80+
return ((InetSocketAddress) getChContext().channel().localAddress()).getAddress().getHostAddress() ;
81+
}
82+
7483
}

src/main/java/org/nlpcn/jcoder/server/rpc/domain/RpcRequest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public class RpcRequest implements Serializable {
99
private String messageId;
1010
private String className;
1111
private String methodName;
12+
private String tokenStr ;
1213
private Object[] arguments;
13-
14+
1415
public RpcRequest() {
1516
}
1617

@@ -46,4 +47,13 @@ public void setArguments(Object[] arguments) {
4647
this.arguments = arguments;
4748
}
4849

50+
public String getTokenStr() {
51+
return tokenStr;
52+
}
53+
54+
public void setTokenStr(String tokenStr) {
55+
this.tokenStr = tokenStr;
56+
}
57+
58+
4959
}

0 commit comments

Comments
 (0)