Skip to content

Migrate ACL from Sa-Token to Spring Security - #4

Open
willzou2012 wants to merge 1 commit into
DCSCDF:main_1from
willzou2012:migrate/sa-token-to-spring-security
Open

willzou2012 wants to merge 1 commit into
DCSCDF:main_1from
willzou2012:migrate/sa-token-to-spring-security

Conversation

@willzou2012

Copy link
Copy Markdown

Summary

Migrate the ACL (authentication + authorization) from Sa-Token to Spring Security. Database tables, permission/role codes, API paths, and response formats are unchanged, so existing clients do not need changes.

What changed

  • Dependency/config: sa-token-spring-boot3-starter → spring-boot-starter-security; sa-token.* properties → app.security.* (token-name, token-timeout, plus the existing active-timeout setting).
  • Authorization: @SaCheckPermission("x") → @PreAuthorize("hasAuthority('x')"), @SaCheckRole("SUPER_ADMIN") → @PreAuthorize("hasRole('SUPER_ADMIN')"). The 56 distinct permission/role values are identical before and after (verified by diffing the annotation sets).
  • Login/sessions: opaque server-side tokens via AuthTokenService + TokenAuthenticationFilter, preserving the previous semantics — token: header (Bearer also accepted), 30-day absolute expiry, 2-hour active timeout when "remember me" is off, single-session login, and forced logout on logout/password-change/disable/delete.
  • User/authority loading: the former StpInterface implementation becomes BlogUserDetailsService (UserDetailsService); group + direct grants and parent-permission expansion are unchanged.
  • Supporting swaps: SaResult → dto/ServiceResult (same code/msg/data semantics), SaTokenDao → ExpiringValueStore (TTL in-memory store for temp tokens/OAuth codes/verification codes), SaFoxUtil → RandomStringUtil, StpUtil call sites → SecurityUtils, Sa-Token exceptions → Spring AuthenticationException/AccessDeniedException with the same unified 401/403 JSON bodies.
  • Docs: added Document/Spring_Security_Migration.md with the full mapping; updated the API docs that referenced Sa-Token.
  • Incidental fix: renamed permissionService.java/permissionServiceImpl.java to match their public class names — required for javac on case-sensitive filesystems (Linux), otherwise annotation processing aborts before Lombok runs.

Behavior notes

  • Tokens remain in-memory (as before): restarting the app invalidates all sessions. AuthTokenService/ExpiringValueStore are the seams to swap for Redis if multi-instance deployment is needed.
  • The token-expired message is unified to 未授权,请先登录 (HTTP 401 and body code unchanged).
  • Known pre-existing quirk (not changed by this PR): the SELECT r.* role query does not populate the superAdmin flag, so the permissions endpoint relies on group/direct grants as the seeded data does.

Test Plan

mvn test (JDK 21): 40 tests run, 0 failures.

  • MyblogDevApplicationTests — pre-existing context test still passes.
  • SpringSecurityMigrationTest (13) — token single-session/expiry, authority expansion (groups, direct, parent, super-admin), filter auth, @PreAuthorize matching, ServiceResult semantics.
  • SecurityFilterChainTest (6) — unified 401, invalid token, public login/logout, CORS preflight, public endpoints not login-blocked.
  • SecurityEndToEndTest (5, H2 + real tokens) — super-admin 200 on admin monitor, normal user 403, profile and permissions endpoints.
  • AuthTokenGlueTest (6) — login/logout/password-change token issuance and invalidation, admin disable/delete force-logout.
  • AuthTempServicesTest (5), GlobalExceptionHandlerSecurityTest (2), PublicCommentSecurityTest (2).

Note: in the sandbox where this was developed, org.wltea:ik-analyzer:2012_u6 is not resolvable from Maven Central, so a local stub of that single class was installed into ~/.m2 to compile/test. That is an environment workaround only and is not part of this change.

Summary:
Replace Sa-Token authentication and authorization with Spring Security
while preserving database tables, permission/role codes, API paths, and
response formats. Login now issues opaque server-side tokens with the
same expiry, active-timeout, single-session, and forced-logout semantics;
@SaCheckPermission/@SaCheckRole become @PreAuthorize hasAuthority/hasRole;
the former StpInterface logic moves to BlogUserDetailsService; SaResult,
SaTokenDao, and SaFoxUtil are replaced by ServiceResult,
ExpiringValueStore, and RandomStringUtil. Also renames two mis-cased
permission service files so javac succeeds on Linux.

Test Plan:
mvn test (JDK 21): 40 tests run, 0 failures. Includes the pre-existing
context test plus new unit and end-to-end coverage for token sessions,
authority expansion, the filter chain, login/logout/password-change
token invalidation, and the temp-token/code services.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant