-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from tomsun28/quarkus-sureness
support protect quarkus feature
- Loading branch information
Showing
33 changed files
with
938 additions
and
44 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
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
67 changes: 67 additions & 0 deletions
67
core/src/main/java/com/usthe/sureness/subject/creater/BasicSubjectJaxRsCreator.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,67 @@ | ||
package com.usthe.sureness.subject.creater; | ||
|
||
import com.usthe.sureness.subject.Subject; | ||
import com.usthe.sureness.subject.SubjectCreate; | ||
import com.usthe.sureness.subject.support.PasswordSubject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* 支持通过basic auth 创建PasswordSubject 的创建者 | ||
* only support JAX-RS | ||
* @author tomsun28 | ||
* @date 23:53 2020-09-20 | ||
*/ | ||
public class BasicSubjectJaxRsCreator implements SubjectCreate { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BasicSubjectJaxRsCreator.class); | ||
|
||
private static final String AUTHORIZATION = "Authorization"; | ||
private static final String BASIC = "Basic"; | ||
private static final int COUNT_2 = 2; | ||
|
||
@Override | ||
public boolean canSupportSubject(Object context) { | ||
// basic auth判断 | ||
// ("Authorization", "Basic YWRtaW46YWRtaW4=") --- basic auth | ||
if (context instanceof ContainerRequestContext) { | ||
String authorization = ((ContainerRequestContext)context).getHeaderString(AUTHORIZATION); | ||
return authorization != null && authorization.startsWith(BASIC); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public Subject createSubject(Object context) { | ||
String authorization = ((ContainerRequestContext)context).getHeaderString(AUTHORIZATION); | ||
//basic auth | ||
String basicAuth = authorization.replace(BASIC, "").trim(); | ||
basicAuth = new String(Base64.getDecoder().decode(basicAuth), StandardCharsets.UTF_8); | ||
String[] auth = basicAuth.split(":"); | ||
if (auth.length != COUNT_2) { | ||
if (logger.isInfoEnabled()) { | ||
logger.info("can not create basic auth PasswordSubject by this request message"); | ||
} | ||
return null; | ||
} | ||
String username = auth[0]; | ||
if (username == null || "".equals(username)) { | ||
if (logger.isInfoEnabled()) { | ||
logger.info("can not create basic auth PasswordSubject by this request message, appId can not null"); | ||
} | ||
return null; | ||
} | ||
String password = auth[1]; | ||
String requestUri = ((ContainerRequestContext) context).getUriInfo().getPath(); | ||
String requestType = ((ContainerRequestContext) context).getMethod(); | ||
String targetUri = requestUri.concat("===").concat(requestType).toLowerCase(); | ||
return PasswordSubject.builder(username, password) | ||
.setTargetResource(targetUri) | ||
.build(); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
core/src/main/java/com/usthe/sureness/subject/creater/JwtSubjectJaxRsCreator.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,64 @@ | ||
package com.usthe.sureness.subject.creater; | ||
|
||
import com.usthe.sureness.subject.Subject; | ||
import com.usthe.sureness.subject.SubjectCreate; | ||
import com.usthe.sureness.subject.support.JwtSubject; | ||
import com.usthe.sureness.util.JsonWebTokenUtil; | ||
import com.usthe.sureness.util.SurenessCommonUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
|
||
|
||
/** | ||
* JwtSubject creator | ||
* only support JAX-RS | ||
* @author tomsun28 | ||
* @date 23:58 2020-02-27 | ||
*/ | ||
public class JwtSubjectJaxRsCreator implements SubjectCreate { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(JwtSubjectJaxRsCreator.class); | ||
|
||
private static final String BEARER = "Bearer"; | ||
private static final String AUTHORIZATION = "Authorization"; | ||
|
||
@Override | ||
public boolean canSupportSubject(Object context) { | ||
// support bearer jwt | ||
// ("Authorization", "Bearer eyJhbGciOiJIUzUxMi...") --- jwt auth | ||
if (context instanceof ContainerRequestContext) { | ||
String authorization = ((ContainerRequestContext)context).getHeaderString(AUTHORIZATION); | ||
if (authorization != null && authorization.startsWith(BEARER)) { | ||
String jwtValue = authorization.replace(BEARER, "").trim(); | ||
return !JsonWebTokenUtil.isNotJsonWebToken(jwtValue); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Subject createSubject(Object context) { | ||
String authorization = ((ContainerRequestContext)context).getHeaderString(AUTHORIZATION); | ||
if (authorization != null && authorization.startsWith(BEARER)) { | ||
// jwt token | ||
String jwtValue = authorization.replace(BEARER, "").trim(); | ||
if (JsonWebTokenUtil.isNotJsonWebToken(jwtValue)) { | ||
if (logger.isInfoEnabled()) { | ||
logger.info("can not create JwtSubject by this request message, is not jwt"); | ||
} | ||
return null; | ||
} | ||
String requestUri = ((ContainerRequestContext) context).getUriInfo().getPath(); | ||
String requestType = ((ContainerRequestContext) context).getMethod(); | ||
String targetUri = requestUri.concat("===").concat(requestType.toLowerCase()); | ||
String userAgent = SurenessCommonUtil.findUserAgent((ContainerRequestContext) context); | ||
return JwtSubject.builder(jwtValue) | ||
.setTargetResource(targetUri) | ||
.setUserAgent(userAgent) | ||
.build(); | ||
} | ||
return null; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
core/src/main/java/com/usthe/sureness/subject/creater/NoneSubjectJaxRsCreator.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,33 @@ | ||
package com.usthe.sureness.subject.creater; | ||
|
||
import com.usthe.sureness.subject.Subject; | ||
import com.usthe.sureness.subject.SubjectCreate; | ||
import com.usthe.sureness.subject.support.NoneSubject; | ||
import com.usthe.sureness.util.SurenessCommonUtil; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
|
||
/** | ||
* 无认证信息的subject creator | ||
* 所有请求都能创建出一个NoneSubject | ||
* only support JAX-RS | ||
* @author tomsun28 | ||
* @date 15:55 2020-02-28 | ||
*/ | ||
public class NoneSubjectJaxRsCreator implements SubjectCreate { | ||
@Override | ||
public boolean canSupportSubject(Object context) { | ||
return context instanceof ContainerRequestContext; | ||
} | ||
|
||
@Override | ||
public Subject createSubject(Object context) { | ||
String requestUri = ((ContainerRequestContext) context).getUriInfo().getPath(); | ||
String requestType = ((ContainerRequestContext) context).getMethod(); | ||
String targetUri = requestUri.concat("===").concat(requestType).toLowerCase(); | ||
String userAgent = SurenessCommonUtil.findUserAgent((ContainerRequestContext) context); | ||
return NoneSubject.builder() | ||
.setTargetUri(targetUri) | ||
.setUserAgent(userAgent).build(); | ||
} | ||
} |
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
Oops, something went wrong.