yonge 5 éve
szülő
commit
a249c07904

+ 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);
+	}
+}

+ 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();
+	}
+}

+ 7 - 1
mec-task/src/main/resources/application.yml

@@ -38,4 +38,10 @@ mybatis:
 task:
   configLocation: classpath:config/properties/quartz.properties
   autoStartup: true
-  startupDelay: 30
+  startupDelay: 30
+  
+security:
+  oauth2:
+    resource:
+      id: task-service
+      access-token-uri: http://auth-server/oauth/token

+ 6 - 6
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>
             <!-- 设置字符集 -->
@@ -177,10 +177,10 @@
 
     <root level="debug">
         <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>
 
     <!--生产环境:输出到文件-->