forked from apache/dubbo-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d13c48
commit 2d38bd9
Showing
4 changed files
with
111 additions
and
33 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...server/src/main/java/org/apache/dubbo/admin/authentication/InterceptorAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dubbo.admin.authentication; | ||
|
||
import org.apache.dubbo.common.extension.SPI; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Permission interception validation | ||
* | ||
*/ | ||
@SPI | ||
public interface InterceptorAuthentication { | ||
|
||
boolean authentication(HttpServletRequest request, HttpServletResponse response, Object handler); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...min-server/src/main/java/org/apache/dubbo/admin/authentication/impl/DefaultPreHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.apache.dubbo.admin.authentication.impl; | ||
|
||
import org.apache.dubbo.admin.annotation.Authority; | ||
import org.apache.dubbo.admin.authentication.InterceptorAuthentication; | ||
import org.apache.dubbo.admin.controller.UserController; | ||
import org.apache.dubbo.admin.interceptor.AuthInterceptor; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.method.HandlerMethod; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.lang.reflect.Method; | ||
|
||
|
||
public class DefaultPreHandle implements InterceptorAuthentication { | ||
//make session timeout configurable | ||
//default to be an hour:1000 * 60 * 60 | ||
@Value("${admin.check.sessionTimeoutMilli:3600000}") | ||
private long sessionTimeoutMilli; | ||
|
||
private AuthInterceptor authInterceptor = new AuthInterceptor(); | ||
|
||
@Override | ||
public boolean authentication(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
HandlerMethod handlerMethod = (HandlerMethod) handler; | ||
Method method = handlerMethod.getMethod(); | ||
Authority authority = method.getDeclaredAnnotation(Authority.class); | ||
if (null == authority) { | ||
authority = method.getDeclaringClass().getDeclaredAnnotation(Authority.class); | ||
} | ||
|
||
String authorization = request.getHeader("Authorization"); | ||
if (null != authority && authority.needLogin()) { | ||
//check if 'authorization' is empty to prevent NullPointException | ||
//since UserController.tokenMap is an instance of ConcurrentHashMap. | ||
if (StringUtils.isEmpty(authorization)) { | ||
//While authentication is required and 'Authorization' string is missing in the request headers, | ||
//reject this request(http403). | ||
authInterceptor.rejectedResponse(response); | ||
return false; | ||
} | ||
|
||
UserController.User user = UserController.tokenMap.get(authorization); | ||
if (null != user && System.currentTimeMillis() - user.getLastUpdateTime() <= sessionTimeoutMilli) { | ||
user.setLastUpdateTime(System.currentTimeMillis()); | ||
return true; | ||
} | ||
|
||
//while user not found, or session timeout, reject this request(http403). | ||
authInterceptor.rejectedResponse(response); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...s/META-INF/dubbo/internal/org.apache.dubbo.admin.authentication.InterceptorAuthentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
defaultHandle=org.apache.dubbo.admin.authentication.impl.DefaultPreHandle |