Browse Source

Merge branch 'master' of https://gitee.com/zouxuan/mec into zouxuan

# Conflicts:
#	mec-task/pom.xml
#	mec-task/src/main/resources/application.yml
zouxuan 5 years ago
parent
commit
cecdb8eda7

+ 3 - 2
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/provider/service/DefaultUserDetailsService.java

@@ -9,7 +9,6 @@ import org.springframework.security.authentication.LockedException;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.AuthorityUtils;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
-import org.springframework.security.core.userdetails.User;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -21,6 +20,7 @@ import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.auth.api.enums.SysUserType;
 import com.ym.mec.auth.config.constant.SecurityConstants;
 import com.ym.mec.auth.service.SysUserService;
+import com.ym.mec.common.security.AuthUser;
 
 @Service
 public class DefaultUserDetailsService implements UserDetailsService {
@@ -67,7 +67,8 @@ public class DefaultUserDetailsService implements UserDetailsService {
 
 		SysUser sysUser = userInfo.getSysUser();
 
-		return new User(username, BCRYPT + sysUser.getPassword(), StringUtils.equals(sysUser.getLockFlag(), "0"), true, true, true, authorities);
+		return new AuthUser(sysUser.getId(), username, BCRYPT + sysUser.getPassword(), StringUtils.equals(sysUser.getLockFlag(), "0"), true, true, true,
+				authorities);
 	}
 
 }

+ 31 - 0
mec-common/src/main/java/com/ym/mec/common/security/AuthUser.java

@@ -0,0 +1,31 @@
+package com.ym.mec.common.security;
+
+import java.util.Collection;
+
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.userdetails.User;
+
+public class AuthUser extends User {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 5532676374055148988L;
+
+	private Integer userId;
+
+	public AuthUser(Integer userId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired,
+			boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
+		super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
+		this.userId = userId;
+	}
+
+	public Integer getUserId() {
+		return userId;
+	}
+
+	public void setUserId(Integer userId) {
+		this.userId = userId;
+	}
+
+}

+ 35 - 0
mec-common/src/main/java/com/ym/mec/common/security/SecurityUtils.java

@@ -0,0 +1,35 @@
+package com.ym.mec.common.security;
+
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+
+public class SecurityUtils {
+	/**
+	 * 获取Authentication
+	 */
+	public Authentication getAuthentication() {
+		return SecurityContextHolder.getContext().getAuthentication();
+	}
+
+	/**
+	 * 获取用户
+	 */
+	public AuthUser getUser(Authentication authentication) {
+		Object principal = authentication.getPrincipal();
+		if (principal instanceof AuthUser) {
+			return (AuthUser) principal;
+		}
+		return null;
+	}
+
+	/**
+	 * 获取用户
+	 */
+	public AuthUser getUser() {
+		Authentication authentication = getAuthentication();
+		if (authentication == null) {
+			return null;
+		}
+		return getUser(authentication);
+	}
+}

+ 2 - 3
mec-task/pom.xml

@@ -63,8 +63,7 @@
 		</dependency>
 		
 	</dependencies>
-	
-	<!--<build>
+	<build>
 		<plugins>
 			<plugin>
 				<groupId>org.springframework.boot</groupId>
@@ -75,5 +74,5 @@
 				<artifactId>docker-maven-plugin</artifactId>
 			</plugin>
 		</plugins>
-	</build>-->
+	</build>
 </project>

+ 16 - 0
mec-task/src/main/java/com/ym/mec/task/jobs/TestTask.java

@@ -0,0 +1,16 @@
+package com.ym.mec.task.jobs;
+
+import org.springframework.stereotype.Service;
+
+import com.ym.mec.task.core.BaseTask;
+import com.ym.mec.task.core.TaskException;
+
+@Service
+public class TestTask extends BaseTask {
+
+	@Override
+	public void execute() throws TaskException {
+		System.out.println("**************** Test  Task ****************");
+	}
+
+}

+ 20 - 0
mec-task/src/main/java/com/ym/mec/task/security/ResourceConfig.java

@@ -0,0 +1,20 @@
+package com.ym.mec.task.security;
+
+import javax.servlet.http.HttpServletResponse;
+
+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;
+import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
+
+@Configuration
+@EnableResourceServer
+public class ResourceConfig extends ResourceServerConfigurerAdapter {
+
+	@Override
+	public void configure(HttpSecurity http) throws Exception {
+		http.csrf().disable().exceptionHandling()
+				.authenticationEntryPoint((request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED)).and()
+				.authorizeRequests().anyRequest().authenticated().and().httpBasic();
+	}
+}

+ 16 - 2
mec-task/src/main/resources/application.yml

@@ -4,7 +4,7 @@ server:
 eureka:
   client:
     serviceUrl:
-      defaultZone: http://localhost:8761/eureka/
+      defaultZone: http://admin:admin123@localhost:8761/eureka/eureka/
 
 spring:
   application:
@@ -34,6 +34,20 @@ spring:
 
 mybatis:
     mapperLocations: classpath:config/mybatis/*.xml
-
+    
+#task:
+  #configLocation: classpath:config/properties/quartz.properties
+  #autoStartup: true
+  #startupDelay: 30
+  
+      
+security:
+  oauth2:
+    client:
+      client-id: app
+      client-secret: app
+    resource:
+      token-info-uri: http://localhost:8001/oauth/check_token
+      
 swagger:
   base-package: com.ym.mec.task.controller

+ 7 - 7
mec-task/src/main/resources/logback-spring.xml

@@ -11,7 +11,7 @@
     <!--<property name="log.path" value="D:/nmyslog/nmys" />-->
     <springProperty scope="context" name="log.path" source="logging.path"/>
 
-    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
+    <property name="CONSOLE_LOG_PATTERN" value="[%X{username} %X{ip} ${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
 
     <!-- 彩色日志 -->
     <!-- 彩色日志依赖的渲染类 -->
@@ -24,9 +24,9 @@
     <!--输出到控制台-->
     <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
         <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
-        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
-            <level>info</level>
-        </filter>
+        <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+            <level>debug</level>
+        </filter> -->
         <encoder>
             <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
             <!-- 设置字符集 -->
@@ -175,12 +175,12 @@
         <logger name="com.ym.mec" level="debug"/>
     </springProfile>
 
-    <root level="debug">
+    <root level="info">
         <appender-ref ref="CONSOLE" />
-        <appender-ref ref="DEBUG_FILE" />
+        <!-- <appender-ref ref="DEBUG_FILE" />
         <appender-ref ref="INFO_FILE" />
         <appender-ref ref="WARN_FILE" />
-        <appender-ref ref="ERROR_FILE" />
+        <appender-ref ref="ERROR_FILE" /> -->
     </root>
 
     <!--生产环境:输出到文件-->