-
Notifications
You must be signed in to change notification settings - Fork 247
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
13b476e
commit 473b4e3
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
32 changes: 32 additions & 0 deletions
32
...rc/main/java/com/alipay/sofa/registry/server/meta/resource/filter/AuthRestController.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,32 @@ | ||
/* | ||
* 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 com.alipay.sofa.registry.server.meta.resource.filter; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import javax.ws.rs.NameBinding; | ||
|
||
/** | ||
* @author jiangcun.hlc | ||
* <p>Nov 17, 2023 | ||
*/ | ||
@NameBinding | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(value = RetentionPolicy.RUNTIME) | ||
public @interface AuthRestController {} |
79 changes: 79 additions & 0 deletions
79
...ta/src/main/java/com/alipay/sofa/registry/server/meta/resource/filter/AuthRestFilter.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,79 @@ | ||
/* | ||
* 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 com.alipay.sofa.registry.server.meta.resource.filter; | ||
|
||
import static com.alipay.sofa.registry.common.model.constants.ValueConstants.ADMIN_API_TOKEN_DATA_ID; | ||
|
||
import com.alipay.sofa.registry.common.model.console.PersistenceData; | ||
import com.alipay.sofa.registry.log.Logger; | ||
import com.alipay.sofa.registry.log.LoggerFactory; | ||
import com.alipay.sofa.registry.server.meta.provide.data.ProvideDataService; | ||
import com.alipay.sofa.registry.store.api.DBResponse; | ||
import com.alipay.sofa.registry.store.api.OperationStatus; | ||
import java.io.IOException; | ||
import javax.annotation.Priority; | ||
import javax.ws.rs.Priorities; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.Provider; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
/** | ||
* @author jiangcun.hlc | ||
* <p>Nov 17, 2023 | ||
*/ | ||
@Provider | ||
@AuthRestController | ||
@Priority(Priorities.USER) | ||
public class AuthRestFilter implements ContainerRequestFilter { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AuthRestFilter.class); | ||
|
||
@Autowired private ProvideDataService provideDataService; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext containerRequestContext) throws IOException { | ||
boolean authAllow; | ||
DBResponse<PersistenceData> queryResponse = | ||
provideDataService.queryProvideData(ADMIN_API_TOKEN_DATA_ID); | ||
if (queryResponse.getOperationStatus() == OperationStatus.SUCCESS) { | ||
authAllow = | ||
StringUtils.equals( | ||
queryResponse.getEntity().getData(), getAuthToken(containerRequestContext)); | ||
} else { | ||
authAllow = true; | ||
} | ||
if (!authAllow) { | ||
Response response = | ||
Response.status(Response.Status.BAD_REQUEST) | ||
.header("reason", "auth check failed!") | ||
.build(); | ||
LOGGER.error( | ||
"[filter] url: %s, auth check fail!", containerRequestContext.getUriInfo().getPath()); | ||
containerRequestContext.abortWith(response); | ||
} | ||
} | ||
|
||
public String getAuthToken(ContainerRequestContext context) { | ||
String token = context.getHeaderString("x-apiauth-token"); | ||
if (StringUtils.isNotBlank(token)) { | ||
return token; | ||
} | ||
return "unknown"; | ||
} | ||
} |