亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

詳解spring cloud ouath2中的資源服務器

瀏覽:100日期:2023-07-23 10:38:21

資源服務器就是業務服務 如用戶服務,訂單服務等 第三方需要到資源服務器調用接口獲取資源

ResourceServerConfig

ResourceServerConfig是資源服務器的核心配置 用于驗證token 與網關配置相似

其中.antMatchers('/**').access('#oauth2.hasScope(’user’)') 需要oauth_client_details表的scope配合 意思是訪問所有資源 需要客戶端有scope需要有user

詳解spring cloud ouath2中的資源服務器

@Configuration@EnableResourceServer // 標識為資源服務器,請求服務中的資源,就要帶著token過來,找不到token或token是無效訪問不了資源@EnableGlobalMethodSecurity(prePostEnabled = true) // 開啟方法級別權限控制public class ResourceServerConfig extends ResourceServerConfigurerAdapter implements CommandLineRunner { private final static Logger logger = LoggerFactory.getLogger(ResourceServerConfig.class); public static final String RESOURCE_ID = 'user'; /** * 權限不足返回給前端json */ @Autowired private CustomAccessDeniedHandlerConfig customAccessDeniedHandlerConfig; @Autowired private TokenStore tokenStore; /** * token無效返回前段json */ @Autowired private AuthExceptionEntryPointConfig authExceptionEntryPointConfig; @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { // 當前資源服務器的資源id,認證服務會認證客戶端有沒有訪問這個資源id的權限,有則可以訪問當前服務 resources.tokenStore(tokenStore).resourceId(RESOURCE_ID) // token無效異常的處理 .authenticationEntryPoint(authExceptionEntryPointConfig) // 權限不足異常處理類 .accessDeniedHandler(customAccessDeniedHandlerConfig) // 會話機制stateless開啟 .stateless(true); } @Override public void configure(HttpSecurity http) throws Exception { http.sessionManagement() // SpringSecurity不會使用也不會創建HttpSession實例 因為整個oauth2后使用token .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests() // 開放swagger請求 .antMatchers('/swagger-ui.html', '/webjars/**', '/swagger-resources/**','/v2/**').permitAll() // 所有請求,都需要有all范圍(scope) .antMatchers('/**').access('#oauth2.hasScope(’user’)'). anyRequest().authenticated().and().csrf() .disable(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); }}

AuthExceptionEntryPointConfig,CustomAccessDeniedHandlerConfig

用于異常返回前端json

@Componentpublic class CustomAccessDeniedHandlerConfig implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { response.setStatus(HttpStatus.OK.value()); response.setHeader('Content-Type', 'application/json;charset=UTF-8'); try { Result result = new Result(403, '權限不足'); response.getWriter().write(new ObjectMapper().writeValueAsString(result)); } catch (IOException e) { e.printStackTrace(); } }}

@Componentpublic class AuthExceptionEntryPointConfig implements AuthenticationEntryPoint{ private final static Logger logger = LoggerFactory.getLogger(AuthExceptionEntryPointConfig.class); @Value('${security.redirect-url}') private String redirectUrl; @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) { Throwable cause = authException.getCause(); response.setStatus(HttpStatus.OK.value()); response.setHeader('Content-Type', 'application/json;charset=UTF-8'); Result result; try { if (cause instanceof InvalidTokenException) { result = new Result(402, '認證失敗,無效或過期token'); response.getWriter().write(new ObjectMapper().writeValueAsString(result)); } else { result = new Result(401, '認證失敗,沒有攜帶token'); response.sendRedirect(redirectUrl); } } catch (IOException e) { e.printStackTrace(); } }}

TokenConfig

不多說

@Configurationpublic class TokenConfig{ /** * 使用redis存儲 */ @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public TokenStore tokenStore() { return new RedisTokenStore(redisConnectionFactory); } }

application.yml

那么小伙伴又問了 既然網關驗證token的有效性 那么資源服務器是不是就不用驗證啦 答案是否 因為不添加配置 會報錯 同樣需要在application中添加以下配置

其他配置也spirng boot為準 這里不多說

security: oauth2: client: client-id: user-vue client-secret: 1234 resource: token-info-uri: http://localhost:8001/oauth/check_token

到此這篇關于spring cloud ouath2中的資源服務器的文章就介紹到這了,更多相關spring cloud ouath2資源服務器內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 综合久久久久6亚洲综合 | 一级看片男女性高爱潮 | 日韩黄色在线观看 | 高清一级毛片免免费看 | 国产xvideos在线观看 | 理论片亚洲 | 亚洲免费在线观看视频 | 欧美日韩亚洲综合久久久 | 黄色网址网站在线观看 | chinese老头 gay tube | 黄色一级录像 | 国内精品一区视频在线播放 | 国产一区二卡三区四区 | 欧美激情一区二区三级高清视频 | 麻豆成人在线观看 | 亚洲在线观看一区二区 | 亚洲国产日韩在线观频 | 久久久久国产一级毛片高清板 | 亚洲色图图 | 欧美日韩国产成人综合在线影院 | 九一视频在线 | 欧洲久久 | 欧美亚洲欧美日韩中文二区 | 99亚洲精品 | 欧美特黄a级高清免费大片 欧美特黄a级高清免费看片 | 国产丰满主播丝袜勾搭秀 | 久久青草免费线观最新 | 精品亚洲一区二区在线播放 | 久草资源在线播放 | 久久精品国产亚洲香蕉 | 成人福利小视频 | 中国护士一级毛片免费版本 | 欧美一级毛片高清视频 | 深夜做爰性大片很黄很色视频 | 羞羞色院91| 亚洲国产精品一区二区第四页 | 亚洲综合伊人色一区 | 国产凹凸一区在线观看视频 | 欧美亚洲国产激情一区二区 | 亚洲精品欧美一区二区三区 | 精品一区二区三区影片 |