Skip to content
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
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ jobs:
dependency_root="${RUNNER_TEMP}/easy4j-dependencies"
mkdir -p "${dependency_root}"
for dependency in ${EASY4J_DEPENDENCIES}; do
case "${dependency}" in
jwt-issuer) pinned_sha="c5873ed3d450f1900dcbf2a4fc2e590077d233f7" ;;
shiro-extension) pinned_sha="2080957579216003a10c36044d312745ae587686" ;;
*) echo "Unexpected dependency: ${dependency}" >&2; exit 1 ;;
esac
git clone --depth 1 --branch "${EASY4J_BRANCH}" \
"https://github.com/easy-4-java/${dependency}.git" \
"${dependency_root}/${dependency}"
git -C "${dependency_root}/${dependency}" fetch --depth 1 origin "${pinned_sha}"
git -C "${dependency_root}/${dependency}" checkout --detach FETCH_HEAD
echo "Pinned ${dependency} at $(git -C "${dependency_root}/${dependency}" rev-parse HEAD)"
for attempt in 1 2 3; do
if (cd "${dependency_root}/${dependency}" && \
bash ./mvnw -B --no-transfer-progress \
Expand All @@ -59,6 +67,9 @@ jobs:
done
done

- name: Record resolved framework dependency tree
run: ./mvnw -B --no-transfer-progress org.apache.maven.plugins:maven-dependency-plugin:3.8.1:tree -Dincludes=org.apache.shiro,org.springframework,jakarta.servlet,javax.servlet

- name: Build and verify with JaCoCo coverage gate
run: ./mvnw -B --no-transfer-progress clean verify

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ JWT authentication and authorization extension for Apache Shiro, built on `shiro
| Item | Version |
| :--- | :--- |
| JDK | 21+ |
| Maven | 3.0+ (Maven Wrapper 3.5.0 bundled) |
| Apache Shiro | 2.2.1 (`shiro-core`, `shiro-web`) |
| Maven | 4.0.0-rc-5 (project Maven Wrapper) |
| Apache Shiro | 3.0.1 (`shiro-core`, `shiro-web`) |
| easy4j dependencies | `shiro-extension-spring`, `jwt-issuer-api` (both `3.0.x.20260630-SNAPSHOT`) |
| JSON | fastjson 2.0.62, jackson-databind 2.17.2 |
| Other | spring-context / spring-web, commons-lang3, guava, javax.servlet-api 4.0.1 |
| JSON | fastjson 2.0.62, Jackson BOM 3.2.1 |
| Other | Spring 7.0.8, Jakarta Servlet 6.1, commons-lang3, guava |

**Version lines**

Expand Down
8 changes: 4 additions & 4 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ Apache Shiro 的 JWT 认证与授权扩展,构建于 `shiro-extension-spring`
| 项目 | 版本 |
| :--- | :--- |
| JDK | 21+ |
| Maven | 3.0+(内置 Maven Wrapper 3.5.0) |
| Apache Shiro | 2.2.1(`shiro-core`、`shiro-web`) |
| Maven | 4.0.0-rc-5(项目 Maven Wrapper) |
| Apache Shiro | 3.0.1(`shiro-core`、`shiro-web`) |
| easy4j 依赖 | `shiro-extension-spring`、`jwt-issuer-api`(均为 `3.0.x.20260630-SNAPSHOT`) |
| JSON | fastjson 2.0.62、jackson-databind 2.17.2 |
| 其他 | spring-context / spring-web、commons-lang3、guava、javax.servlet-api 4.0.1 |
| JSON | fastjson 2.0.62、Jackson BOM 3.2.1 |
| 其他 | Spring 7.0.8、Jakarta Servlet 6.1、commons-lang3、guava |

**版本线**

Expand Down
6 changes: 2 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
<jackson-bom.version>3.2.1</jackson-bom.version>
<jakarta-servlet-api.version>6.1.0</jakarta-servlet-api.version>
<lombok.version>1.18.46</lombok.version>
<shiro.version>2.2.1</shiro.version>
<shiro.version>3.0.1</shiro.version>
<!-- Dependency versions -->
<slf4j.version>2.0.18</slf4j.version>
<spring.version>5.3.30</spring.version>
<spring.version>7.0.8</spring.version>
<maven-central-publishing-plugin.version>0.11.0</maven-central-publishing-plugin.version>
<maven-compiler-plugin.version>3.15.0</maven-compiler-plugin.version>
<maven-gpg-plugin.version>3.2.8</maven-gpg-plugin.version>
Expand Down Expand Up @@ -97,13 +97,11 @@
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
1 change: 0 additions & 1 deletion shiro-jwt-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
1 change: 0 additions & 1 deletion shiro-jwt-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.apache.shiro.jwt.authc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.WriteListener;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.jwt.token.JwtAuthorizationToken;
import org.junit.jupiter.api.Test;

class JwtAuthenticatingFilterRequestTest {

private final RequestFilter filter = new RequestFilter();

@Test
void readsJwtFromServletHeaderParameterAndCookie() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader("X-Authorization")).thenReturn("header-token", (String) null, (String) null);
when(request.getParameter("token")).thenReturn("param-token", (String) null);
when(request.getCookies()).thenReturn(new Cookie[] {new Cookie("token", "cookie-token")});

assertThat(filter.getAccessToken(request)).isEqualTo("header-token");
assertThat(filter.getAccessToken(request)).isEqualTo("param-token");
assertThat(filter.getAccessToken(request)).isEqualTo("cookie-token");
assertThat(filter.isJwtSubmission(request, mock(HttpServletResponse.class))).isTrue();
}

@Test
void createsJwtAuthorizationTokenFromRequest() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader("X-Authorization")).thenReturn("jwt-value");
when(request.getRemoteAddr()).thenReturn("192.0.2.11");

JwtAuthorizationToken token = (JwtAuthorizationToken) filter.createJwtToken(request,
mock(HttpServletResponse.class));

assertThat(token.getToken()).isEqualTo("jwt-value");
assertThat(token.getHost()).isEqualTo("192.0.2.11");
}

@Test
void missingJwtWritesUnauthorizedJsonResponse() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
when(response.getOutputStream()).thenReturn(new ServletOutputStream() {
@Override public boolean isReady() { return true; }
@Override public void setWriteListener(WriteListener listener) { }
@Override public void write(int value) throws IOException { bytes.write(value); }
});

assertThat(filter.onAccessDenied(request, response)).isFalse();
verify(response).setContentType("application/json");
assertThat(bytes.toString("UTF-8")).contains("401");
}

private static class RequestFilter extends JwtAuthenticatingFilter {
@Override
protected boolean isLoginRequest(ServletRequest request, ServletResponse response) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.apache.shiro.jwt.authz;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.WriteListener;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.jwt.token.JwtAuthorizationToken;
import org.apache.shiro.subject.Subject;
import org.junit.jupiter.api.Test;

class JwtAuthorizationFilterRequestTest {

private final JwtAuthorizationFilter filter = new JwtAuthorizationFilter();

@Test
void readsTokenFromHeaderThenParameterThenCookie() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader("X-Authorization")).thenReturn("header-token", (String) null, (String) null);
when(request.getParameter("token")).thenReturn("param-token", (String) null);
when(request.getCookies()).thenReturn(new Cookie[] {new Cookie("token", "cookie-token")});

assertThat(filter.getAccessToken(request)).isEqualTo("header-token");
assertThat(filter.getAccessToken(request)).isEqualTo("param-token");
assertThat(filter.getAccessToken(request)).isEqualTo("cookie-token");
assertThat(filter.isJwtSubmission(request, mock(HttpServletResponse.class))).isTrue();
}

@Test
void createsJwtTokenFromServletHeaderAndRemoteAddress() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader("X-Authorization")).thenReturn("jwt-value");
when(request.getRemoteAddr()).thenReturn("192.0.2.10");

JwtAuthorizationToken token = (JwtAuthorizationToken) filter.createJwtToken(request,
mock(HttpServletResponse.class));

assertThat(token.getToken()).isEqualTo("jwt-value");
assertThat(token.getHost()).isEqualTo("192.0.2.10");
}

@Test
void validServletJwtLogsInSubjectAndAllowsRequest() throws Exception {
Subject subject = mock(Subject.class);
JwtAuthorizationFilter filterWithSubject = new JwtAuthorizationFilter() {
@Override
protected Subject getSubject(jakarta.servlet.ServletRequest request,
jakarta.servlet.ServletResponse response) {
return subject;
}
};
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader("X-Authorization")).thenReturn("jwt-value");
when(request.getRemoteAddr()).thenReturn("192.0.2.10");

assertThat(filterWithSubject.isAccessAllowed(request, mock(HttpServletResponse.class), null)).isTrue();
verify(subject).login(any(JwtAuthorizationToken.class));
}

@Test
void missingTokenWritesUnauthorizedJsonResponse() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
when(response.getOutputStream()).thenReturn(new ServletOutputStream() {
@Override public boolean isReady() { return true; }
@Override public void setWriteListener(WriteListener listener) { }
@Override public void write(int value) throws IOException { bytes.write(value); }
});

assertThat(filter.isAccessAllowed(request, response, null)).isFalse();
verify(response).setContentType("application/json");
assertThat(bytes.toString("UTF-8")).contains("401");
}
}
Loading