Commit b2f71862 authored by 董天德's avatar 董天德

Merge branch 'dev_20230205_mq'

parents c5e36455 f8f1a058
package com.hungraim.ltc.controller; package com.hungraim.ltc.controller;
import com.alibaba.fastjson.JSONObject;
import com.hungraim.ltc.util.CsoftSecurityUtil; import com.hungraim.ltc.util.CsoftSecurityUtil;
import com.hungraim.ltc.util.Result; import com.hungraim.ltc.util.Result;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.springframework.security.core.Authentication; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint; import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal; import java.security.Principal;
import java.util.Map; import java.util.Map;
...@@ -26,6 +24,7 @@ import java.util.Map; ...@@ -26,6 +24,7 @@ import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
public class AuthController { public class AuthController {
private final RedisTemplate redisTemplate;
private final TokenEndpoint tokenEndpoint; private final TokenEndpoint tokenEndpoint;
@PostMapping("/token") @PostMapping("/token")
...@@ -48,15 +47,12 @@ public class AuthController { ...@@ -48,15 +47,12 @@ public class AuthController {
return Result.success(keyMap.get(0).toString()); return Result.success(keyMap.get(0).toString());
} }
@GetMapping("/authLogout") @PostMapping("/logout")
public Result authLogout(HttpServletRequest request, HttpServletResponse response) { public Result doLogout(String access_token){
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String claims = JwtHelper.decode(access_token).getClaims();
//清除认证 String jti = (String)JSONObject.parseObject(claims).get("jti");
if (auth != null) { redisTemplate.opsForValue().set(jti,access_token);
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return Result.success(); return Result.success();
} }
} }
...@@ -12,6 +12,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur ...@@ -12,6 +12,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.LogoutHandler; import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
...@@ -20,7 +21,6 @@ import org.springframework.security.web.authentication.logout.SecurityContextLog ...@@ -20,7 +21,6 @@ import org.springframework.security.web.authentication.logout.SecurityContextLog
*/ */
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
@Order(1) @Order(1)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...@@ -35,13 +35,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -35,13 +35,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll() .authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
.and() .and()
.authorizeRequests() .authorizeRequests()
.antMatchers("/getPublicKey","/oauth/*").permitAll() .antMatchers("/getPublicKey","/oauth/**").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated();
.and()
.logout()
.logoutUrl("/oauth/logout")
.invalidateHttpSession(true);
} }
/** /**
......
...@@ -56,6 +56,13 @@ ...@@ -56,6 +56,13 @@
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId> <artifactId>spring-security-oauth2-resource-server</artifactId>
</dependency> </dependency>
<!--jwt-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.0.9.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
......
...@@ -51,7 +51,7 @@ public class ResourceServerConfig { ...@@ -51,7 +51,7 @@ public class ResourceServerConfig {
http.oauth2ResourceServer().jwt() http.oauth2ResourceServer().jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter()); .jwtAuthenticationConverter(jwtAuthenticationConverter());
http.authorizeExchange() http.authorizeExchange()
.pathMatchers("/api/oauth/token","/oauth/*","/api/oauth/genKeyPair").permitAll() .pathMatchers("/api/oauth/token","/oauth/**","/api/oauth/genKeyPair").permitAll()
.anyExchange().access(authorizationManager) .anyExchange().access(authorizationManager)
.and() .and()
.exceptionHandling() .exceptionHandling()
...@@ -61,6 +61,7 @@ public class ResourceServerConfig { ...@@ -61,6 +61,7 @@ public class ResourceServerConfig {
.authenticationEntryPoint(authenticationEntryPoint()) .authenticationEntryPoint(authenticationEntryPoint())
.and().csrf().disable(); .and().csrf().disable();
return http.build(); return http.build();
} }
......
...@@ -3,6 +3,7 @@ package com.hungraim.ltc.gateway.security; ...@@ -3,6 +3,7 @@ package com.hungraim.ltc.gateway.security;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.hungraim.ltc.constant.AuthConstants; import com.hungraim.ltc.constant.AuthConstants;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -13,6 +14,7 @@ import org.springframework.security.authorization.AuthorizationDecision; ...@@ -13,6 +14,7 @@ import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.ReactiveAuthorizationManager; import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.web.server.authorization.AuthorizationContext; import org.springframework.security.web.server.authorization.AuthorizationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
...@@ -45,6 +47,7 @@ public class AuthorizationManager implements ReactiveAuthorizationManager<Author ...@@ -45,6 +47,7 @@ public class AuthorizationManager implements ReactiveAuthorizationManager<Author
public Mono<AuthorizationDecision> check(Mono<Authentication> mono, AuthorizationContext authorizationContext) { public Mono<AuthorizationDecision> check(Mono<Authentication> mono, AuthorizationContext authorizationContext) {
ServerHttpRequest request = authorizationContext.getExchange().getRequest(); ServerHttpRequest request = authorizationContext.getExchange().getRequest();
String path = request.getMethodValue() + "_" + request.getURI().getPath(); String path = request.getMethodValue() + "_" + request.getURI().getPath();
log.info("请求,path={}", path); log.info("请求,path={}", path);
...@@ -62,6 +65,14 @@ public class AuthorizationManager implements ReactiveAuthorizationManager<Author ...@@ -62,6 +65,14 @@ public class AuthorizationManager implements ReactiveAuthorizationManager<Author
log.info("请求token为空拒绝访问,path={}", path); log.info("请求token为空拒绝访问,path={}", path);
return Mono.just(new AuthorizationDecision(false)); return Mono.just(new AuthorizationDecision(false));
} }
String subToken = token.substring(AuthConstants.JWT_PREFIX.length());
String claims = JwtHelper.decode(subToken).getClaims();
String jti = (String) JSONObject.parseObject(claims).get("jti");
String acc_token = (String) redisTemplate.opsForValue().get(jti);
if (!StrUtil.isBlank(acc_token)) {
log.info("请求token为空拒绝访问,path={}", path);
return Mono.just(new AuthorizationDecision(false));
}
// 从缓存取资源权限角色关系列表 // 从缓存取资源权限角色关系列表
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment