管理员角色也允许登录前台用户端

This commit is contained in:
lxp
2026-10-22 16:48:37 +08:00
parent 961a61afb4
commit 01aa13935a
10 changed files with 156 additions and 27 deletions
@@ -40,7 +40,8 @@ public class BearerTokenFilter extends OncePerRequestFilter {
Long id = session == null ? null : session.userId();
if (id == null) throw new BusinessException(401, "UNAUTHORIZED", "登录已失效,请重新登录");
com.linkerbot.vibemake.bean.entity.User user = users.require(id);
if (!"USER".equals(user.getRole()) || user.getSessionVersion() != session.version())
if ((!"USER".equals(user.getRole()) && !"ADMIN".equals(user.getRole()))
|| user.getSessionVersion() != session.version())
throw new BusinessException(401, "UNAUTHORIZED", "登录已失效,请重新登录");
UsernamePasswordAuthenticationToken auth =
new UsernamePasswordAuthenticationToken(
@@ -1,15 +1,57 @@
package com.linkerbot.vibemake.security;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.config.RedisStateProperties;
import com.linkerbot.vibemake.service.config.VibemakeProperties;
import com.linkerbot.vibemake.service.state.RedisStateStore;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
class SessionTest {
@Test
@SuppressWarnings("unchecked")
void communityAndAdminTokensForTheSameAccountCannotCrossApplications() {
Map<String, String> stored = new HashMap<>();
StringRedisTemplate redis = mock(StringRedisTemplate.class);
ValueOperations<String, String> values = mock(ValueOperations.class);
when(redis.opsForValue()).thenReturn(values);
when(values.setIfAbsent(anyString(), anyString(), any(Duration.class)))
.thenAnswer(input -> stored.putIfAbsent(input.getArgument(0), input.getArgument(1)) == null);
when(values.get(anyString())).thenAnswer(input -> stored.get(input.getArgument(0)));
RedisStateProperties communityNamespace = new RedisStateProperties();
communityNamespace.setApplication("api");
RedisStateProperties adminNamespace = new RedisStateProperties();
adminNamespace.setApplication("admin");
TokenService community =
new TokenService(
new VibemakeProperties(),
new RedisStateStore(redis, communityNamespace),
new ObjectMapper());
TokenService admin =
new TokenService(
new VibemakeProperties(),
new RedisStateStore(redis, adminNamespace),
new ObjectMapper());
String communityToken = community.issue(8, 3);
String adminToken = admin.issue(8, 3);
assertThat(community.resolve(communityToken)).isEqualTo(8L);
assertThat(admin.resolve(adminToken)).isEqualTo(8L);
assertThat(admin.session(communityToken)).isNull();
assertThat(community.session(adminToken)).isNull();
}
@Test
void sharedStateSurvivesServiceReplacementAndStoresNoRawToken() {
com.linkerbot.vibemake.testing.InMemoryStateStore state =