src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java
@@ -42,7 +42,7 @@ .antMatchers("/swagger**/**").permitAll() .antMatchers("/webjars/**").permitAll() .antMatchers("/v2/**").permitAll() .antMatchers("/api/common/**").permitAll() .antMatchers("/api/common/login").permitAll() .anyRequest().authenticated() .and().apply(securityConfiguereAdapter()); } src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java
@@ -18,10 +18,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -52,4 +49,14 @@ return iCommonService.login(adminLoginDto); } /** * 用户退出登录 * @return */ @ApiOperation(value="用户退出登录", notes="用户退出登录") @GetMapping(value = "/Logout/{id}") public Result memberLogout(@PathVariable(value = "id") Long id) { return iCommonService.memberLogout(id); } } src/main/java/com/xcong/farmer/cms/modules/system/service/ICommonService.java
@@ -9,4 +9,5 @@ Result login(AdminLoginDto adminLoginDto); Result memberLogout(Long id); } src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java
@@ -8,12 +8,15 @@ import com.xcong.farmer.cms.common.response.Result; import com.xcong.farmer.cms.configurations.properties.ApplicationProperties; import com.xcong.farmer.cms.configurations.properties.SecurityProperties; import com.xcong.farmer.cms.modules.member.entity.MemberEntity; import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto; import com.xcong.farmer.cms.modules.system.entity.UserEntity; import com.xcong.farmer.cms.modules.system.mapper.UserMapper; import com.xcong.farmer.cms.modules.system.service.ICommonService; import com.xcong.farmer.cms.utils.MessageSourceUtils; import com.xcong.farmer.cms.utils.RedisUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @@ -69,6 +72,21 @@ return Result.ok("登录成功", authInfo); } @Override public Result memberLogout(Long id) { //获取用户ID UserEntity userEntity = userMapper.selectById(id); if (ObjectUtil.isEmpty(userEntity)) { return Result.fail("用户不存在"); } String redisMember = AppContants.APP_LOGIN_PREFIX + userEntity.getId(); String token = redisUtils.getString(redisMember); redisUtils.del(AppContants.APP_LOGIN_PREFIX + token); SecurityContextHolder.clearContext(); return Result.ok("退出成功"); } public String generateAsaToken(String token) { RSA rsa = new RSA(null, securityProperties.getPublicKey()); return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);