♻️ 重构代码。@EnablePigxResoureServer 一个注解接入OAuth
This commit is contained in:
1 parent
44ac88de6e
commit
584569ce3c
25 files changed
+151
-169
No files matched your search
+34
-1
@@ -1,8 +1,41 @@
|
||||
package com.pig4cloud.pigx.common.security;
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.annotation;
|
||||
|
||||
import com.pig4cloud.pigx.common.security.component.PigxResourceServerAutoConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/11/10
|
||||
* <p>
|
||||
* 资源服务注解
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@EnableResourceServer
|
||||
@Import(PigxResourceServerAutoConfiguration.class)
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
public @interface EnablePigxResourceServer {
|
||||
}
|
||||
+24
-3
@@ -17,11 +17,16 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
||||
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
|
||||
@@ -36,7 +41,11 @@ import org.springframework.web.client.RestTemplate;
|
||||
* 1. 支持remoteTokenServices 负载均衡
|
||||
* 2. 支持 获取用户全部信息
|
||||
*/
|
||||
public abstract class BaseResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
|
||||
@Autowired
|
||||
protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
|
||||
@Autowired
|
||||
@@ -45,16 +54,28 @@ public abstract class BaseResourceServerConfigurerAdapter extends ResourceServer
|
||||
protected RemoteTokenServices remoteTokenServices;
|
||||
@Autowired
|
||||
protected UserDetailsService userDetailsService;
|
||||
@Autowired
|
||||
private PermitAllUrlProperties permitAllUrlProperties;
|
||||
|
||||
|
||||
/**
|
||||
* 默认的配置,对外暴露
|
||||
*
|
||||
* @param http
|
||||
* @param httpSecurity
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public abstract void configure(HttpSecurity http) throws Exception;
|
||||
public void configure(HttpSecurity httpSecurity) throws Exception {
|
||||
//允许使用iframe 嵌套,避免swagger-ui 不被加载的问题
|
||||
httpSecurity.headers().frameOptions().disable();
|
||||
ExpressionUrlAuthorizationConfigurer<HttpSecurity>
|
||||
.ExpressionInterceptUrlRegistry registry = httpSecurity
|
||||
.authorizeRequests();
|
||||
permitAllUrlProperties.getIgnoreUrls()
|
||||
.forEach(url -> registry.antMatchers(url).permitAll());
|
||||
registry.anyRequest().authenticated()
|
||||
.and().csrf().disable();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供子类重写
|
||||
|
||||
+3
-3
@@ -33,8 +33,8 @@ import java.util.List;
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConditionalOnExpression("!'${ignore}'.isEmpty()")
|
||||
@ConfigurationProperties(prefix = "ignore")
|
||||
@ConditionalOnExpression("!'${security.oauth2.client.ignore-urls}'.isEmpty()")
|
||||
@ConfigurationProperties(prefix = "security.oauth2.client")
|
||||
public class PermitAllUrlProperties {
|
||||
private List<String> urls = new ArrayList<>();
|
||||
private List<String> ignoreUrls = new ArrayList<>();
|
||||
}
|
||||
+21
-1
@@ -1,8 +1,28 @@
|
||||
package com.pig4cloud.pigx.common.security;
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/11/10
|
||||
*/
|
||||
@ComponentScan("com.pig4cloud.pigx.common.security")
|
||||
public class PigxResourceServerAutoConfiguration {
|
||||
}
|
||||
Reference in new issue
Block a user