Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support springboot3 version #185

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ When use maven or gradle build project, add coordinate
<dependency>
<groupId>com.usthe.sureness</groupId>
<artifactId>sureness-core</artifactId>
<version>1.0.8</version>
<version>1.1.0</version>
</dependency>
```
```
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '1.0.8'
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '1.1.0'
```

#### 🐵 Use the Default Configuration to Configure Sureness
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@
<dependency>
<groupId>com.usthe.sureness</groupId>
<artifactId>sureness-core</artifactId>
<version>1.0.8</version>
<version>1.1.0</version>
</dependency>
```
```
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '1.0.8'
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '1.1.0'
```

#### 🐵 使用默认配置来配置Sureness
Expand Down
15 changes: 11 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.usthe.sureness</groupId>
<artifactId>sureness-core</artifactId>
<version>1.0.8</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>sureness</name>
Expand Down Expand Up @@ -40,15 +40,16 @@
scm:git:https://github.com/dromara/sureness.git
</developerConnection>
<url>https://github.com/dromara/sureness</url>
<tag>1.0.8</tag>
<tag>1.1.0</tag>
</scm>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javax.servlet.version>3.1.0</javax.servlet.version>
<jakarta.servlet.version>6.0.0</jakarta.servlet.version>
<old.servlet.version>4.0.1</old.servlet.version>
<javax.ws.rs.version>2.1.1</javax.ws.rs.version>
<jjwt.version>0.11.2</jjwt.version>
<yaml.version>1.26</yaml.version>
Expand All @@ -58,10 +59,16 @@
<easymock.version>4.0.2</easymock.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jakarta.servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<version>${old.servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class DefaultSurenessConfig {
private static final Logger logger = LoggerFactory.getLogger(DefaultSurenessConfig.class);

public static final String SUPPORT_SERVLET = "servlet";
public static final String SUPPORT_JAKARTA_SERVLET = "jakarta-servlet";
public static final String SUPPORT_JAX_RS = "jax-rs";
public static final String SUPPORT_SPRING_REACTIVE = "spring-reactive";

Expand Down Expand Up @@ -94,6 +95,13 @@ private void init(String supportContainer) {
new BasicSubjectSpringReactiveCreator(),
new JwtSubjectSpringReactiveCreator(),
new JwtSubjectWsSpringReactiveCreator());
} else if (SUPPORT_JAKARTA_SERVLET.equals(supportContainer)) {
subjectCreates = Arrays.asList(
new NoneSubjectJakartaServletCreator(),
new DigestSubjectJakartaServletCreator(),
new BasicSubjectJakartaServletCreator(),
new JwtSubjectJakartaServletCreator(),
new JwtSubjectJakartaServletCreator());
} else {
subjectCreates = Arrays.asList(
new NoneSubjectServletCreator(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 com.usthe.sureness.util.ServletUtil;
import com.usthe.sureness.util.SurenessConstant;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
* the subject creator support creating PasswordSubject
* only support HttpServletRequest
* @author tomsun28
* @date 23:53 2020-02-27
*/
public class BasicSubjectJakartaServletCreator implements SubjectCreate {

private static final Logger logger = LoggerFactory.getLogger(BasicSubjectJakartaServletCreator.class);

private static final int COUNT_2 = 2;

@Override
public boolean canSupportSubject(Object context) {
// ("Authorization", "Basic YWRtaW46YWRtaW4=") --- basic auth
if (context instanceof HttpServletRequest) {
String authorization = ((HttpServletRequest)context).getHeader(SurenessConstant.AUTHORIZATION);
return authorization != null && authorization.startsWith(SurenessConstant.BASIC);
} else {
return false;
}
}

@Override
public Subject createSubject(Object context) {
String authorization = ((HttpServletRequest)context).getHeader(SurenessConstant.AUTHORIZATION);
//basic auth
String basicAuth = authorization.replace(SurenessConstant.BASIC, "").trim();
try {
basicAuth = new String(Base64.getDecoder().decode(basicAuth), StandardCharsets.UTF_8);
} catch (Exception e) {
if (logger.isInfoEnabled()) {
logger.info("can not create basic auth PasswordSubject, due {}", e.getMessage());
}
return null;
}
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;
}
username = username.trim();
String password = auth[1] == null ? null : auth[1].trim();
String remoteHost = ((HttpServletRequest) context).getRemoteHost();
String requestUri = ServletUtil.getRequestUri((HttpServletRequest) context);
String requestType = ((HttpServletRequest) context).getMethod();
String targetUri = requestUri.concat("===").concat(requestType).toLowerCase();
return PasswordSubject.builder(username, password)
.setRemoteHost(remoteHost)
.setTargetResource(targetUri)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.usthe.sureness.subject.creater;

import com.usthe.sureness.subject.Subject;
import com.usthe.sureness.subject.SubjectCreate;
import com.usthe.sureness.subject.support.DigestSubject;
import com.usthe.sureness.util.ServletUtil;
import com.usthe.sureness.util.SurenessConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* the subject creator support creating DigestSubject
* only support HttpServletRequest
* @author tomsun28
* @date 2020-10-28 20:44
*/
public class DigestSubjectJakartaServletCreator implements SubjectCreate {

private static final Logger logger = LoggerFactory.getLogger(DigestSubjectJakartaServletCreator.class);

private static final String USERNAME = "username";
private static final String NONCE = "nonce";
private static final String QOP = "qop";
private static final String REALM = "realm";
private static final String NC = "nc";
private static final String CNONCE = "cnonce";
private static final String RESPONSE = "response";
private static final String URI = "uri";
private static final int FILED_SIZE = 2;
private static final String SPLIT = "\"";

@Override
public boolean canSupportSubject(Object context) {
if (context instanceof HttpServletRequest) {
String authorization = ((HttpServletRequest)context).getHeader(SurenessConstant.AUTHORIZATION);
return authorization == null || authorization.startsWith(SurenessConstant.DIGEST);
} else {
return false;
}
}

@Override
public Subject createSubject(Object context) {
String authorization = ((HttpServletRequest)context).getHeader(SurenessConstant.AUTHORIZATION);
if (authorization == null) {
return new DigestSubject();
} else {
// digest auth
String digestAuth = authorization.replace(SurenessConstant.DIGEST, "").trim();
try {
final Map<String, String> digestMap = new HashMap<>(8);
Arrays.stream(digestAuth.split(",")).forEach(auth -> {
String[] tmpArr = auth.trim().split("=");
if (tmpArr.length == FILED_SIZE) {
String authValue = tmpArr[1].trim();
if (authValue.startsWith(SPLIT) && authValue.endsWith(SPLIT)) {
authValue = authValue.substring(1, authValue.length() - 1);
}
digestMap.put(tmpArr[0].trim(), authValue);
}
});
String username = digestMap.get(USERNAME);
String response = digestMap.get(RESPONSE);
String realm = digestMap.get(REALM);
String uri = digestMap.get(URI);
String nonce = digestMap.get(NONCE);
String nc = digestMap.get(NC);
String cNonce = digestMap.get(CNONCE);
String qop = digestMap.get(QOP);
if (username == null || response == null || realm == null || uri == null
|| nonce == null || nc == null || cNonce == null) {
logger.debug("can not create digest subject due some need field is null");
return null;
}
String remoteHost = ((HttpServletRequest) context).getRemoteHost();
String requestUri = ServletUtil.getRequestUri((HttpServletRequest) context);
String requestType = ((HttpServletRequest) context).getMethod();
String targetUri = requestUri.concat("===").concat(requestType).toLowerCase();
return DigestSubject.builder(username, response)
.setRealm(realm).setUri(uri).setNonce(nonce)
.setNc(nc).setCnonce(cNonce).setQop(qop).setHttpMethod(requestType.toUpperCase())
.setRemoteHost(remoteHost).setTargetUri(targetUri)
.build();
} catch (Exception e) {
logger.info("create digest subject error happen, due {}", e.getMessage(), e);
return null;
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.ServletUtil;
import com.usthe.sureness.util.SurenessConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.http.HttpServletRequest;

/**
* the subject creator support creating JwtSubject
* only support HttpServletRequest
* @author tomsun28
* @date 23:58 2020-02-27
*/
public class JwtSubjectJakartaServletCreator implements SubjectCreate {

private static final Logger logger = LoggerFactory.getLogger(JwtSubjectJakartaServletCreator.class);

@Override
public boolean canSupportSubject(Object context) {
// support bearer jwt
// ("Authorization", "Bearer eyJhbGciOiJIUzUxMi...") --- jwt auth
if (context instanceof HttpServletRequest) {
String authorization = ((HttpServletRequest)context).getHeader(SurenessConstant.AUTHORIZATION);
if (authorization != null && authorization.startsWith(SurenessConstant.BEARER)) {
String jwtValue = authorization.replace(SurenessConstant.BEARER, "").trim();
return !JsonWebTokenUtil.isNotJsonWebToken(jwtValue);
}
}
return false;
}

@Override
public Subject createSubject(Object context) {
String authorization = ((HttpServletRequest)context).getHeader(SurenessConstant.AUTHORIZATION);
if (authorization != null && authorization.startsWith(SurenessConstant.BEARER)) {
// jwt token
String jwtValue = authorization.replace(SurenessConstant.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 remoteHost = ((HttpServletRequest) context).getRemoteHost();
String requestUri = ServletUtil.getRequestUri((HttpServletRequest) context);
String requestType = ((HttpServletRequest) context).getMethod();
String targetUri = requestUri.concat("===").concat(requestType.toLowerCase());
return JwtSubject.builder(jwtValue)
.setRemoteHost(remoteHost)
.setTargetResource(targetUri)
.build();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.ServletUtil;
import com.usthe.sureness.util.SurenessConstant;

import jakarta.servlet.http.HttpServletRequest;

/**
* the subject creator support creating JwtSubject from websocket
* only support HttpServletRequest
* @author tomsun28
* @date 2021/3/2 22:14
*/
public class JwtSubjectWsJakartaServletCreator implements SubjectCreate {

@Override
public boolean canSupportSubject(Object context) {
// support token jwt
// requestUri?token=jwt0-eyJhbGciOiJIUzUxMi --- jwt auth
if (context instanceof HttpServletRequest) {
String token = ((HttpServletRequest)context).getParameter(SurenessConstant.TOKEN);
return !JsonWebTokenUtil.isNotJsonWebToken(token);
}
return false;
}

@Override
public Subject createSubject(Object context) {
// support token jwt
// requestUri?token=jwt0-eyJhbGciOiJIUzUxMi --- jwt auth
String jwtToken = ((HttpServletRequest)context).getParameter(SurenessConstant.TOKEN);
if (jwtToken != null) {
jwtToken = jwtToken.trim();
String remoteHost = ((HttpServletRequest) context).getRemoteHost();
String requestUri = ServletUtil.getRequestUri((HttpServletRequest) context);
String requestType = ((HttpServletRequest) context).getMethod();
String targetUri = requestUri.concat("===").concat(requestType.toLowerCase());
return JwtSubject.builder(jwtToken)
.setRemoteHost(remoteHost)
.setTargetResource(targetUri)
.build();
}
return null;
}
}
Loading
Loading