fix: restore miniapp public startup flows
This commit is contained in:
+1
-1
@@ -25,7 +25,7 @@ public class ActivityController {
|
|||||||
@GetMapping
|
@GetMapping
|
||||||
public ApiResponse<?> getActivities(
|
public ApiResponse<?> getActivities(
|
||||||
@AuthenticationPrincipal UserPrincipal principal) {
|
@AuthenticationPrincipal UserPrincipal principal) {
|
||||||
String userId = principal.id();
|
String userId = principal == null ? null : principal.id();
|
||||||
return ApiResponse.ok(activityService.getActivities(userId));
|
return ApiResponse.ok(activityService.getActivities(userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ public class SecurityConfig {
|
|||||||
.requestMatchers("/auth/wechat/login", "/auth/refresh", "/auth/operator/login").permitAll()
|
.requestMatchers("/auth/wechat/login", "/auth/refresh", "/auth/operator/login").permitAll()
|
||||||
.requestMatchers("/health/**").permitAll()
|
.requestMatchers("/health/**").permitAll()
|
||||||
.requestMatchers("/version").permitAll()
|
.requestMatchers("/version").permitAll()
|
||||||
|
.requestMatchers("/app/config").permitAll()
|
||||||
.requestMatchers("/activities").permitAll()
|
.requestMatchers("/activities").permitAll()
|
||||||
.requestMatchers("/auth/**").authenticated()
|
.requestMatchers("/auth/**").authenticated()
|
||||||
// 操作台接口需要认证
|
// 操作台接口需要认证
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.danyang.match.platform.security;
|
||||||
|
|
||||||
|
import com.danyang.match.platform.activity.service.ActivityService;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class PublicMiniappApiTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private ActivityService activityService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void appConfigIsAvailableWithoutLogin() throws Exception {
|
||||||
|
mockMvc.perform(get("/api/v1/app/config").contextPath("/api/v1"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.success").value(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void activityListIsAvailableWithoutLogin() throws Exception {
|
||||||
|
when(activityService.getActivities(null)).thenReturn(List.of());
|
||||||
|
|
||||||
|
mockMvc.perform(get("/api/v1/activities").contextPath("/api/v1"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.success").value(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,13 +35,18 @@ class AuthStore extends BaseStore {
|
|||||||
/** 冷启动从 storage 还原登录态 */
|
/** 冷启动从 storage 还原登录态 */
|
||||||
hydrate() {
|
hydrate() {
|
||||||
const snapshot = BaseStore.hydrate(STORAGE_KEYS.IS_LOGGED_IN);
|
const snapshot = BaseStore.hydrate(STORAGE_KEYS.IS_LOGGED_IN);
|
||||||
|
if (!api.getToken()) {
|
||||||
|
try { wx.removeStorageSync(STORAGE_KEYS.IS_LOGGED_IN); } catch (e) {}
|
||||||
|
this.setState({ isLoggedIn: false, userId: null, phoneBound: false });
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (snapshot && typeof snapshot === 'object') {
|
if (snapshot && typeof snapshot === 'object') {
|
||||||
this.setState({
|
this.setState({
|
||||||
isLoggedIn: !!snapshot.isLoggedIn,
|
isLoggedIn: !!snapshot.isLoggedIn,
|
||||||
userId: snapshot.userId || null,
|
userId: snapshot.userId || null,
|
||||||
phoneBound: !!snapshot.phoneBound,
|
phoneBound: !!snapshot.phoneBound,
|
||||||
});
|
});
|
||||||
} else if (api.getToken()) {
|
} else {
|
||||||
// 兼容旧版仅存了 token 的场景
|
// 兼容旧版仅存了 token 的场景
|
||||||
this.setState({ isLoggedIn: true });
|
this.setState({ isLoggedIn: true });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import { createRequire } from 'node:module'
|
||||||
|
|
||||||
|
const removed = []
|
||||||
|
globalThis.wx = {
|
||||||
|
getStorageSync(key) {
|
||||||
|
if (key === 'isLoggedIn') return { isLoggedIn: true, userId: 'stale-user' }
|
||||||
|
return ''
|
||||||
|
},
|
||||||
|
removeStorageSync(key) {
|
||||||
|
removed.push(key)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const require = createRequire(import.meta.url)
|
||||||
|
const authStore = require('../miniapp/stores/authStore.js')
|
||||||
|
authStore.hydrate()
|
||||||
|
|
||||||
|
assert.equal(authStore.state.isLoggedIn, false, '没有 access token 时不得恢复登录态')
|
||||||
|
assert.ok(removed.includes('isLoggedIn'), '应清理失真的登录快照')
|
||||||
|
|
||||||
|
console.log('Miniapp auth contracts passed')
|
||||||
Reference in New Issue
Block a user