diff --git a/mss-common/src/main/java/com/microservice/skeleton/common/vo/RoleVo.java b/mss-common/src/main/java/com/microservice/skeleton/common/vo/RoleVo.java new file mode 100644 index 0000000000000000000000000000000000000000..8de6f2df61f607deda0f1a1a73dcb661ffe6da4f --- /dev/null +++ b/mss-common/src/main/java/com/microservice/skeleton/common/vo/RoleVo.java @@ -0,0 +1,22 @@ +package com.microservice.skeleton.common.vo; + +import lombok.Data; + +import java.io.Serializable; + +/** + * Created with IntelliJ IDEA. + * Description: + * User: Mr.Yangxiufeng + * Date: 2018-05-10 + * Time: 21:03 + */ +@Data +public class RoleVo implements Serializable { + private static final long serialVersionUID = 2179037393108205286L; + private Integer roleId; + + private String name; + + private String value; +} diff --git a/mss-gateway/pom.xml b/mss-gateway/pom.xml index f82811c3852eb9026410316c70d8645351e811ae..3154dc7e5601317208f19d4fd6b87927db4998ba 100644 --- a/mss-gateway/pom.xml +++ b/mss-gateway/pom.xml @@ -42,10 +42,10 @@ org.springframework.cloud spring-cloud-starter-feign - - org.springframework.boot - spring-boot-starter-actuator - + + + + io.springfox springfox-swagger2 diff --git a/mss-gateway/src/main/java/com/microservice/skeleton/gateway/GatewayApplication.java b/mss-gateway/src/main/java/com/microservice/skeleton/gateway/GatewayApplication.java index a7b9173c0f53bed248cb084869c6981252567efe..71986acd21b7be709f052733005db99974c34a74 100644 --- a/mss-gateway/src/main/java/com/microservice/skeleton/gateway/GatewayApplication.java +++ b/mss-gateway/src/main/java/com/microservice/skeleton/gateway/GatewayApplication.java @@ -4,10 +4,12 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; @SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy +@EnableGlobalMethodSecurity(prePostEnabled = true) public class GatewayApplication { public static void main(String[] args) { diff --git a/mss-gateway/src/main/java/com/microservice/skeleton/gateway/config/SecurityConfig.java b/mss-gateway/src/main/java/com/microservice/skeleton/gateway/config/SecurityConfig.java index 3e23a9b7572c645a4a89627228b3fe1fbd55c57b..4541e793038cee4e025b649bd19e46535293d010 100644 --- a/mss-gateway/src/main/java/com/microservice/skeleton/gateway/config/SecurityConfig.java +++ b/mss-gateway/src/main/java/com/microservice/skeleton/gateway/config/SecurityConfig.java @@ -4,6 +4,7 @@ import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; /** * Created by Mr.Yangxiufeng on 2017/12/29. @@ -11,7 +12,8 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur * ProjectName:Mirco-Service-Skeleton */ @Configuration -@EnableOAuth2Sso +//@EnableOAuth2Sso +//@EnableResourceServer public class SecurityConfig extends WebSecurityConfigurerAdapter{ @Override diff --git a/mss-gateway/src/main/java/com/microservice/skeleton/gateway/filter/PreRequestFilter.java b/mss-gateway/src/main/java/com/microservice/skeleton/gateway/filter/PreRequestFilter.java index 02b741da7f5c52ecf9492b2961a3dc84b69050f3..83fc8013acb9f8917b45d539e45194a63b9117d0 100644 --- a/mss-gateway/src/main/java/com/microservice/skeleton/gateway/filter/PreRequestFilter.java +++ b/mss-gateway/src/main/java/com/microservice/skeleton/gateway/filter/PreRequestFilter.java @@ -4,6 +4,8 @@ import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @@ -38,6 +40,7 @@ public class PreRequestFilter extends ZuulFilter { public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); LOG.info("send {} request to {}",request.getMethod(),request.getRequestURL().toString()); return null; } diff --git a/mss-gateway/src/main/resources/application.yml b/mss-gateway/src/main/resources/application.yml index 360a5632f609a04e005fe4ff083ae8acc8361f7a..753b63fb53357f06e674b53d642b54b0ddf1670d 100644 --- a/mss-gateway/src/main/resources/application.yml +++ b/mss-gateway/src/main/resources/application.yml @@ -17,14 +17,14 @@ eureka: service-url: defaultZone: http://mss-eureka1:9010/eureka/,http://mss-eureka2:9011/eureka/ ###actuator监控点 start#### -endpoints: - health: - sensitive: false - enabled: true +#endpoints: +# health: +# sensitive: false +# enabled: true ##默认情况下很多端点是不允许访问的,会返回401:Unauthorized -management: - security: - enabled: false +#management: +# security: +# enabled: false ###actuator监控点 end#### ###如下Oauth2.0配置参考## @@ -42,17 +42,23 @@ zuul: strip-prefix: true sensitiveHeaders: serviceId: auth2.0-center -security: - basic: - enabled: false - oauth2: - client: - ##网关的地址 - access-token-uri: http://localhost:9030/uaa/oauth/token - user-authorization-uri: http://localhost:9030/uaa/oauth/authorize - resource: - user-info-uri: http://localhost:9030/uaa/user - prefer-token-info: false +#security: +# basic: +# enabled: false +# oauth2: +# client: +# ##网关的地址 +# access-token-uri: http://localhost:9030/uaa/oauth/token +# user-authorization-uri: http://localhost:9030/uaa/oauth/authorize +# resource: +# user-info-uri: http://localhost:9060/user +# prefer-token-info: false +#security: +# oauth2: +# resource: +# id: resource +# user-info-uri: http://localhost:9060/user +# prefer-token-info: false ##############end##################### ####超时配置#### ribbon: diff --git a/mss-resource/.gitignore b/mss-modules/mss-resource/.gitignore similarity index 100% rename from mss-resource/.gitignore rename to mss-modules/mss-resource/.gitignore diff --git a/mss-resource/pom.xml b/mss-modules/mss-resource/pom.xml similarity index 92% rename from mss-resource/pom.xml rename to mss-modules/mss-resource/pom.xml index 5624ab432e2e6b9a4b6ccc995c808e657e6151ba..48f67262a12b8a2595db6618635380c66ad8872e 100644 --- a/mss-resource/pom.xml +++ b/mss-modules/mss-resource/pom.xml @@ -3,15 +3,15 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.microservice.skeleton - Micro-Service-Skeleton-Parent + com.microservice.skeleton.modules + Micro-Service-Skeleton-modules 0.0.1-SNAPSHOT Micro-Service-Skeleton-Resource 0.0.1-SNAPSHOT jar - resource + mss-resource Demo project for Spring Boot diff --git a/mss-resource/src/main/java/com/microservice/skeleton/resource/ResourceApplication.java b/mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/ResourceApplication.java similarity index 100% rename from mss-resource/src/main/java/com/microservice/skeleton/resource/ResourceApplication.java rename to mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/ResourceApplication.java diff --git a/mss-resource/src/main/java/com/microservice/skeleton/resource/config/ResourceServerConfig.java b/mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/config/ResourceServerConfig.java similarity index 78% rename from mss-resource/src/main/java/com/microservice/skeleton/resource/config/ResourceServerConfig.java rename to mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/config/ResourceServerConfig.java index d9061a28c3f0dcb39e5674213bf311a10e5506c7..38799dcd89860429142e4a2bc3152eed09ca0932 100644 --- a/mss-resource/src/main/java/com/microservice/skeleton/resource/config/ResourceServerConfig.java +++ b/mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/config/ResourceServerConfig.java @@ -1,6 +1,5 @@ package com.microservice.skeleton.resource.config; -import org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; @@ -18,9 +17,6 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter { public void configure(HttpSecurity http) throws Exception { http. csrf().disable() - .exceptionHandling() - .authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\"")) - .and() .authorizeRequests() .mvcMatchers("/v2/api-docs").permitAll() .anyRequest().authenticated() diff --git a/mss-resource/src/main/java/com/microservice/skeleton/resource/config/Swagger2Config.java b/mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/config/Swagger2Config.java similarity index 100% rename from mss-resource/src/main/java/com/microservice/skeleton/resource/config/Swagger2Config.java rename to mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/config/Swagger2Config.java diff --git a/mss-resource/src/main/java/com/microservice/skeleton/resource/controller/UserController.java b/mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/controller/UserController.java similarity index 100% rename from mss-resource/src/main/java/com/microservice/skeleton/resource/controller/UserController.java rename to mss-modules/mss-resource/src/main/java/com/microservice/skeleton/resource/controller/UserController.java diff --git a/mss-modules/mss-resource/src/main/resources/application.yml b/mss-modules/mss-resource/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..f973ef4c558889b5b01cda3dc1fb177468cd6600 --- /dev/null +++ b/mss-modules/mss-resource/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server: + port: 9023 +spring: + application: + name: resource + zipkin: + base-url: http://localhost:9050 +eureka: + instance: + prefer-ip-address: true + instance-id: ${spring.cloud.client.ipAddress}:${server.port} + ##续约更新时间间隔设置5秒,m默认30s + lease-renewal-interval-in-seconds: 5 + ##续约到期时间10秒,默认是90秒 + lease-expiration-duration-in-seconds: 10 + client: + service-url: + defaultZone: http://mss-eureka1:9010/eureka/,http://mss-eureka2:9011/eureka/ +endpoints: + health: + sensitive: false + enabled: true +management: + security: + enabled: false +security: + oauth2: + resource: + id: resource + user-info-uri: http://localhost:9030/uaa/user + prefer-token-info: false \ No newline at end of file diff --git a/mss-resource/src/test/java/com/microservice/skeleton/resource/ResourceApplicationTests.java b/mss-modules/mss-resource/src/test/java/com/microservice/skeleton/resource/ResourceApplicationTests.java similarity index 100% rename from mss-resource/src/test/java/com/microservice/skeleton/resource/ResourceApplicationTests.java rename to mss-modules/mss-resource/src/test/java/com/microservice/skeleton/resource/ResourceApplicationTests.java diff --git a/mss-modules/mss-upms/src/main/java/com/microservice/skeleton/upms/service/impl/UserServiceImpl.java b/mss-modules/mss-upms/src/main/java/com/microservice/skeleton/upms/service/impl/UserServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..505be6c2c7271f76e76f4ad14095d1195bc3221e --- /dev/null +++ b/mss-modules/mss-upms/src/main/java/com/microservice/skeleton/upms/service/impl/UserServiceImpl.java @@ -0,0 +1,31 @@ +package com.microservice.skeleton.upms.service.impl; + +import com.microservice.skeleton.upms.entity.RcUser; +import com.microservice.skeleton.upms.mapper.RcUserMapper; +import com.microservice.skeleton.upms.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import tk.mybatis.mapper.entity.Example; +import tk.mybatis.mapper.util.Sqls; + +/** + * Created with IntelliJ IDEA. + * Description: + * User: Mr.Yangxiufeng + * Date: 2018-05-10 + * Time: 19:57 + */ +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private RcUserMapper userMapper; + + @Override + public RcUser findByUsername(String username) { + Example example = Example.builder(RcUser.class) + .where(Sqls.custom().andEqualTo("username",username)) + .build(); + return userMapper.selectOneByExample(example); + } +} diff --git a/mss-oauth/src/main/java/com/microservice/skeleton/auth/config/ResourceServerConfig.java b/mss-oauth/src/main/java/com/microservice/skeleton/auth/config/ResourceServerConfig.java index 08da64bf174a16606200e637aa3b9af5eb645186..f5122eb96a3271e5c40eef5cf70f966957c65b98 100644 --- a/mss-oauth/src/main/java/com/microservice/skeleton/auth/config/ResourceServerConfig.java +++ b/mss-oauth/src/main/java/com/microservice/skeleton/auth/config/ResourceServerConfig.java @@ -1,6 +1,5 @@ package com.microservice.skeleton.auth.config; -import org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; @@ -19,9 +18,6 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter{ public void configure(HttpSecurity http) throws Exception { http. csrf().disable() - .exceptionHandling() - .authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\"")) - .and() .authorizeRequests().anyRequest().authenticated() .and() .httpBasic(); diff --git a/pom.xml b/pom.xml index 1537389321dce6f21d232e9a3f8f1c92ac77a3e1..28a898ccddc8c4e45cea92535fbbcc0065221a4e 100644 --- a/pom.xml +++ b/pom.xml @@ -19,18 +19,21 @@ 1.1.6 2.8.0 1.16.20 + 2.2.0 + 2.0.2 org.springframework.boot spring-boot-starter-parent - 1.5.9.RELEASE + 1.5.12.RELEASE + mss-common mss-eureka mss-oauth mss-gateway mss-monitor - mss-resource + mss-modules