diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99f55fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*/*.iml +*/target +*/.idea +*/out +*/out/* diff --git a/README.md b/README.md index c4842c2..af3fa46 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,127 @@ -# MineKnowContainer +个人知识小仓库,方便以后查阅。(如有错误,麻烦请指出,谢谢) -## Java多线程 +Ps:以前什么都想看看,都是什么也弄不深,后悔ing。不过写了就懒得删了...... + +## Java +#### Java JVM +- [Java JVM:总结](https://github.com/pzxwhc/MineKnowContainer/issues/63) +- [Java JVM:内存结构和相关参数含义](https://github.com/pzxwhc/MineKnowContainer/issues/24) +- [Java JVM:对象是如何创建以及定位](https://github.com/pzxwhc/MineKnowContainer/issues/62) +- [Java JVM:垃圾回收(GC 在什么时候,对什么东西,做了什么事情)](https://github.com/pzxwhc/MineKnowContainer/issues/26) +- [Java JVM:垃圾收集器(转载)](https://github.com/pzxwhc/MineKnowContainer/issues/61) +- [Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 unable to create native thread)](https://github.com/pzxwhc/MineKnowContainer/issues/25) +- [Java JVM:编译加载机制与自定义类加载器](https://github.com/pzxwhc/MineKnowContainer/issues/27) + +#### Java多线程 - [Java 多线程:多线程生成的原因(Java内存模型与i++操作解析)](https://github.com/pzxwhc/MineKnowContainer/issues/6) - [Java 多线程:生产者消费者问题](https://github.com/pzxwhc/MineKnowContainer/issues/8) -- [Java 多线程:synchronized 关键字用法(修饰类,方法,静态方法,代码块)](https://github.com/pzxwhc/MineKnowContainer/issues/7) -- [Java 多线程:condition关键字](https://github.com/pzxwhc/MineKnowContainer/issues/10) -- [Java 多线程:volatile关键字](https://github.com/pzxwhc/MineKnowContainer/issues/11) -- [Java 多线程:threadlocal关键字](https://github.com/pzxwhc/MineKnowContainer/issues/12) +- [Java 多线程:Synchronized 关键字用法(修饰类,方法,静态方法,代码块)](https://github.com/pzxwhc/MineKnowContainer/issues/7) +- [Java 多线程:Lock接口(接口方法分析,ReentrantLock,ReadWriteLock)](https://github.com/pzxwhc/MineKnowContainer/issues/16) +- [Java 多线程:Condition关键字](https://github.com/pzxwhc/MineKnowContainer/issues/10) +- [Java 多线程:Volatile关键字](https://github.com/pzxwhc/MineKnowContainer/issues/11) +- [Java 多线程:Threadlocal关键字](https://github.com/pzxwhc/MineKnowContainer/issues/12) +- [Java 多线程:InheritableThreadLocal 实现原理](https://github.com/pzxwhc/MineKnowContainer/issues/20) +- [Java 多线程:CAS 与 AtomicInteger(乐观锁)](https://github.com/pzxwhc/MineKnowContainer/issues/17) - [Java 多线程:线程池实现原理](https://github.com/pzxwhc/MineKnowContainer/issues/9) +#### Java集合 +- [Java 集合总结(Collection系列与Map系列)](https://github.com/pzxwhc/MineKnowContainer/issues/75) +- [Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)](https://github.com/pzxwhc/MineKnowContainer/issues/18) +- [Java 集合:HashMap(put方法的实现 与 哈希冲突)](https://github.com/pzxwhc/MineKnowContainer/issues/19) +- [Java 集合:HashSet,HashTable,以及 ConcurrentHashMap 概念](https://github.com/pzxwhc/MineKnowContainer/issues/21) +- [Java 集合:LinkedHashMap工作原理及实现(转载)](https://github.com/pzxwhc/MineKnowContainer/issues/76) +- [Java 集合:TreeMap工作原理及实现(转载)](https://github.com/pzxwhc/MineKnowContainer/issues/77) + +#### Java IO +- [Java IO:操作系统的IO处理过程以及5种网络IO模型](https://github.com/pzxwhc/MineKnowContainer/issues/22) +- [Java IO:常见Java IO流介绍(ByteArrayInputStream,ObjectInputStream,BufferedInputStream)](https://github.com/pzxwhc/MineKnowContainer/issues/23) + +#### Java 其他 +- [Java 其他:异常体系结构与 try-catch-finally](https://github.com/pzxwhc/MineKnowContainer/issues/56) + ## 设计模式 - [设计模式 -- Builder模式(多个构造器参数时可显著改善可读性)](https://github.com/pzxwhc/MineKnowContainer/issues/2) - [设计模式 -- 装饰器模式(为对象动态的添加功能)](https://github.com/pzxwhc/MineKnowContainer/issues/3) - [设计模式 -- 策略模式(对象的某个行为,在不同的场景中,有不同的实现算法)](https://github.com/pzxwhc/MineKnowContainer/issues/4) - [设计模式 -- 观察者模式(有利于代码解耦)](https://github.com/pzxwhc/MineKnowContainer/issues/5) +- [设计模式 -- 工厂模式(简单工厂模式,工厂方法模式,抽象工厂模式)](https://github.com/pzxwhc/MineKnowContainer/issues/73) +- [设计模式 -- 单例模式(饿汉,懒汉模式与多线程下的使用)](https://github.com/pzxwhc/MineKnowContainer/issues/74) + +## 排序算法 +- [排序算法:选择排序](https://github.com/pzxwhc/MineKnowContainer/issues/43) +- [排序算法:冒泡排序](https://github.com/pzxwhc/MineKnowContainer/issues/44) +- [排序算法:插入排序](https://github.com/pzxwhc/MineKnowContainer/issues/45) +- [排序算法:希尔排序](https://github.com/pzxwhc/MineKnowContainer/issues/46) +- [排序算法:快速排序](https://github.com/pzxwhc/MineKnowContainer/issues/51) + +## Mysql +- [Mysql:行级锁](https://github.com/pzxwhc/MineKnowContainer/issues/71) + +## HTTP +- [HTTP:Referer](https://github.com/pzxwhc/MineKnowContainer/issues/60) + +## 安全 +- [安全:CSRF 攻击](https://github.com/pzxwhc/MineKnowContainer/issues/64) +- [安全:XSS 攻击](https://github.com/pzxwhc/MineKnowContainer/issues/65) +- [安全:公钥,私钥 以及部署 HTTPS](https://github.com/pzxwhc/MineKnowContainer/issues/66) + +## 框架技术 +#### Spring 与 SpringMVC +- [Spring:AOP(JDK动态代理与CGLIB代理)](https://github.com/pzxwhc/MineKnowContainer/issues/28) +- [Spring:DI原理(包括如何创建实体和具体注入)](https://github.com/pzxwhc/MineKnowContainer/issues/30) +- [Spring:异常统一处理的三种方式与Rest接口异常的处理](https://github.com/pzxwhc/MineKnowContainer/issues/29) +- [Spring:事务的传播属性,隔离级别以及脏读,幻读,不可重复读概念](https://github.com/pzxwhc/MineKnowContainer/issues/31) +- [SpringMVC:FrameworkServlet(主要用于初始化WebApplicationContext)](https://github.com/pzxwhc/MineKnowContainer/issues/34) +- [SpringMVC:DispatcherServlet(流程)](https://github.com/pzxwhc/MineKnowContainer/issues/35) +- [SpringMVC:前后端传值(包括@PathVarible,@PathParam等)](https://github.com/pzxwhc/MineKnowContainer/issues/36) + +#### Spring Security +- [Spring Security:相关类以及 Authentication 认证流程(基于SpringBoot)](https://github.com/pzxwhc/MineKnowContainer/issues/58) +- [Spring Security:结合 Spring-OAuth,支持 Mysql 数据库(基于SpringBoot)](https://github.com/pzxwhc/MineKnowContainer/issues/59) + +#### Quartz +- [Quartz:Spring + Quartz 支持多机任务调度(实现和原理流程)](https://github.com/pzxwhc/MineKnowContainer/issues/37) +- [Quartz:使用行级锁实现集群](https://github.com/pzxwhc/MineKnowContainer/issues/69) +- [Quartz:错误处理与监控](https://github.com/pzxwhc/MineKnowContainer/issues/70) + +#### RabbitMQ +- [RabbitMQ:作用,模型,重要概念](https://github.com/pzxwhc/MineKnowContainer/issues/38) +- [RabbitMQ:常用三种交换机(Direct,Topic,Fanout)](https://github.com/pzxwhc/MineKnowContainer/issues/42) +- [RabbitMQ:Hello World Demo(基于 spring,非原生模式)](https://github.com/pzxwhc/MineKnowContainer/issues/47) +- [RabbitMQ:Spring对RabbitMQ的注解支持(@EnableRabbit,@RabbitListener,@RabbitHandler)](https://github.com/pzxwhc/MineKnowContainer/issues/48) +- [RabbitMQ:Publisher的消息确认机制](https://github.com/pzxwhc/MineKnowContainer/issues/49) +- [RabbitMQ:Consumer的ack机制 和 死信(Dead Letter)](https://github.com/pzxwhc/MineKnowContainer/issues/50) + +#### NodeJs +- [NodeJs:require,exports,module 概念](https://github.com/pzxwhc/MineKnowContainer/issues/39) +- [NodeJs:async 模块(同时发送多个请求)](https://github.com/pzxwhc/MineKnowContainer/issues/40) +- [NodeJs:使用 winston 作为日志记录](https://github.com/pzxwhc/MineKnowContainer/issues/53) + +#### Nginx +- [Nginx:对静态资源的处理和直接访问 node 差不多?](https://github.com/pzxwhc/MineKnowContainer/issues/67) + +#### Docker +- [Docker:4种网络模式以及bridge模式下容器的通信](https://github.com/pzxwhc/MineKnowContainer/issues/52) + +#### OAuth +- [OAuth2:四种基本授权模式](https://github.com/pzxwhc/MineKnowContainer/issues/41) +- [OAuth2:授权码模式 Java 实现版本](https://github.com/pzxwhc/MineKnowContainer/issues/54) +- [OAuth2:使用Spring-OAuth,基于Spring-Security,SpringBoot,支持 Mysql 数据库](https://github.com/pzxwhc/MineKnowContainer/issues/59) +- [OAuth2:忽略 state 参数引发的 csrf 漏洞](https://github.com/pzxwhc/MineKnowContainer/issues/68) + +#### Web +- [Web:初步理解 Tomcat](https://github.com/pzxwhc/MineKnowContainer/issues/55) +- [Web:Servlet 体系结构与 ServletContext,ServletConfig](https://github.com/pzxwhc/MineKnowContainer/issues/32) + +## 杂谈 +- [杂谈:NodeJs 和 Servlet 的对比](https://github.com/pzxwhc/MineKnowContainer/issues/57) ## 工具 - [iTerm 常用命令](https://github.com/pzxwhc/MineKnowContainer/issues/1) - [vim 常用命令](https://github.com/pzxwhc/MineKnowContainer/issues/13) + +# 最后 + +- 个人博客:[https://www.wuhuachuan.com/](https://www.wuhuachuan.com/) +- 知乎:[https://www.zhihu.com/people/wuhuachuan](https://www.zhihu.com/people/wuhuachuan) +- 微博:[点这里](http://weibo.com/p/1005051796766555/home?from=page_100505&mod=TAB&is_all=1#place) diff --git a/Stack/Test/MyStackTest.java b/Stack/Test/MyStackTest.java new file mode 100644 index 0000000..4fc0a89 --- /dev/null +++ b/Stack/Test/MyStackTest.java @@ -0,0 +1,75 @@ +import org.junit.Test; + +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + +/** + * Created by wuhuachuan on 16/8/25. + */ +public class MyStackTest { + private static final int LENGTH = 3; + private MyStack myStack = new MyStack(3); + + @Test + public void testPush(){ + //1. null + try { + myStack.push(null); + assertFalse(true); + } catch (IllegalArgumentException e){ + assertTrue(true); + } + + //2. int 类型 + try { + myStack.push(1); + assertTrue(true); + } catch (IllegalArgumentException e){ + assertTrue(true); + } + + //3. 合理的push + Integer element = new Integer(2); + myStack.push(element); + assertTrue("push fail.",testDataIsExist(element)); + + //4. 出界 + try { + for(int i = 0; i < LENGTH + 1; ++i){ + myStack.push(new Integer(i)); + } + assertFalse(true); + } catch (Exception e){ + assertTrue(true); + } + } + + @Test + public void testPop(){ + //1. 数组元素为空的时候 pop + try { + myStack.pop(); + assertFalse(true); + } catch (Exception e){ + assertTrue(true); + } + + //2. 正常 pop // PS:需要先保证 push 操作 ok + Integer element = new Integer(1); + myStack.push(element); + assertTrue("pop is wrong",myStack.pop() == element); + } + + private boolean testDataIsExist(Integer element) { + Object elementData[] = myStack.getElementData(); + + for (int i = 0 ; i < elementData.length; ++i){ + if(element == elementData[i]){ + return true; + } else { + ; + } + } + return false; + } +} \ No newline at end of file diff --git a/Stack/java-hamcrest-2.0.0.0.jar b/Stack/java-hamcrest-2.0.0.0.jar new file mode 100644 index 0000000..7310af1 Binary files /dev/null and b/Stack/java-hamcrest-2.0.0.0.jar differ diff --git a/Stack/junit-4.12.jar b/Stack/junit-4.12.jar new file mode 100644 index 0000000..3a7fc26 Binary files /dev/null and b/Stack/junit-4.12.jar differ diff --git a/Stack/src/MyStack.java b/Stack/src/MyStack.java new file mode 100644 index 0000000..37beaa1 --- /dev/null +++ b/Stack/src/MyStack.java @@ -0,0 +1,41 @@ +public class MyStack { + + private static final int DEFAULT_LENGTH = 5; + private Object elementData[]; + + private int size = 0; + + + public MyStack(){ + elementData = new Object[DEFAULT_LENGTH]; + } + public MyStack(int length){ + elementData = new Object[length]; + } + + public void push(T element) throws ArrayIndexOutOfBoundsException,IllegalArgumentException { + if(element == null){ + throw new IllegalArgumentException("element can not be null"); + } else { + if(size == elementData.length){ + throw new ArrayIndexOutOfBoundsException(); + } else { + elementData[size++] = element; + } + } + } + + public T pop() throws ArrayIndexOutOfBoundsException{ + if(size == 0){ + throw new ArrayIndexOutOfBoundsException(); + } else { + T element = (T) elementData[--size]; + elementData[size] = null; // 防止内存泄露 + return element; + } + } + + public Object[] getElementData(){ + return elementData; + } +} diff --git a/apache_oltu_oauth2_demo_codeScheme/pom.xml b/apache_oltu_oauth2_demo_codeScheme/pom.xml new file mode 100644 index 0000000..16e0ac9 --- /dev/null +++ b/apache_oltu_oauth2_demo_codeScheme/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.oauth.code + com.oauth.code + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + 1.16.6 + + + + org.apache.oltu.oauth2 + org.apache.oltu.oauth2.authzserver + 1.0.1 + + + org.apache.oltu.oauth2 + org.apache.oltu.oauth2.client + 1.0.1 + + + commons-httpclient + commons-httpclient + 3.1 + + + + \ No newline at end of file diff --git a/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/Boot.java b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/Boot.java new file mode 100644 index 0000000..07911bf --- /dev/null +++ b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/Boot.java @@ -0,0 +1,18 @@ +package com.oauth.code; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * Created by wuhuachuan on 16/5/9. + */ +@EnableAutoConfiguration +@ComponentScan +@Configuration +public class Boot { + public static void main(String[] args) throws Exception { + SpringApplication.run(Boot.class, args); + } +} diff --git a/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/Config.java b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/Config.java new file mode 100644 index 0000000..7263dfa --- /dev/null +++ b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/Config.java @@ -0,0 +1,25 @@ +package com.oauth.code; + +import org.apache.oltu.oauth2.as.issuer.OAuthIssuer; +import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; +import org.apache.oltu.oauth2.as.issuer.UUIDValueGenerator; +import org.apache.oltu.oauth2.as.issuer.ValueGenerator; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by wuhuachuan on 16/5/9. + */ +@Configuration +public class Config { + + @Bean + public ValueGenerator createGenerator(){ + return new UUIDValueGenerator(); + } + + @Bean + public OAuthIssuer createIssuer(ValueGenerator valueGenerator){ + return new OAuthIssuerImpl(valueGenerator); + } +} diff --git a/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/OAuthServer.java b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/OAuthServer.java new file mode 100644 index 0000000..40fb5ce --- /dev/null +++ b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/OAuthServer.java @@ -0,0 +1,133 @@ +package com.oauth.code; + +import org.apache.oltu.oauth2.as.issuer.OAuthIssuer; +import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest; +import org.apache.oltu.oauth2.as.request.OAuthTokenRequest; +import org.apache.oltu.oauth2.as.response.OAuthASResponse; +import org.apache.oltu.oauth2.common.exception.OAuthProblemException; +import org.apache.oltu.oauth2.common.exception.OAuthSystemException; +import org.apache.oltu.oauth2.common.message.OAuthResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * Created by wuhuachuan on 16/5/9. + */ + +@RestController +public class OAuthServer { + + @Autowired + private OAuthIssuer oAuthIssuer; + + //token 过期时间 + private final String expires = "3600"; + + /** + * 生成 code,并且返回给 第三方应用 指定的 redirect_url + * @param httpServletRequest + * @param response + * @throws OAuthProblemException + * @throws OAuthSystemException + * @throws IOException + */ + @RequestMapping(value = "/oauthserver/get-code",method = RequestMethod.GET) + public void getCode(HttpServletRequest httpServletRequest, HttpServletResponse response) + throws OAuthProblemException, OAuthSystemException, IOException { + + try { + //dynamically recognize an OAuth profile based on request characteristic (params, + // method, content type etc.), perform validation + OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(httpServletRequest); + + validateRedirectionURI(oauthRequest); + + //build OAuth response + OAuthResponse resp = OAuthASResponse + .authorizationResponse(httpServletRequest,HttpServletResponse.SC_FOUND) + .setCode(oAuthIssuer.authorizationCode()) + .location(oauthRequest.getRedirectURI()) + .buildQueryMessage(); + + response.sendRedirect(resp.getLocationUri()); + + } catch (OAuthProblemException ex){ + final OAuthResponse resp = OAuthASResponse + .errorResponse(HttpServletResponse.SC_FOUND) + .error(ex) + .location(ex.getRedirectUri()) + .buildQueryMessage(); + + response.sendRedirect(resp.getLocationUri()); + } + } + + private void validateRedirectionURI(OAuthAuthzRequest oauthRequest) { + } + + + /** + * 生成 token,并且通过 HttpServletResponse 把 token 有关信息返回给 客户端 + * @param httpServletRequest + * @param httpServletResponse + * @throws OAuthSystemException + * @throws IOException + */ + @RequestMapping(value = "/oauthserver/get-token",method = RequestMethod.POST) + public void getToken(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) + throws OAuthSystemException, IOException { + + try { + OAuthTokenRequest oauthRequest = new OAuthTokenRequest(httpServletRequest); + + validateClient(oauthRequest); + + // generator token + String accessToken = oAuthIssuer.accessToken(); + String refreshToken = oAuthIssuer.refreshToken(); + + // some code + OAuthResponse r = OAuthASResponse + .tokenResponse(HttpServletResponse.SC_OK) + .setAccessToken(accessToken) + .setExpiresIn(expires) + .setRefreshToken(refreshToken) + .buildBodyMessage(); + + httpServletResponse.setStatus(r.getResponseStatus()); + PrintWriter pw = httpServletResponse.getWriter(); + pw.print(r.getBody()); + pw.flush(); + pw.close(); + + //if something goes wrong + } catch(OAuthProblemException ex) { + + OAuthResponse r = OAuthResponse + .errorResponse(401) + .error(ex) + .buildJSONMessage(); + + httpServletResponse.setStatus(r.getResponseStatus()); + + PrintWriter pw = httpServletResponse.getWriter(); + pw.print(r.getBody()); + pw.flush(); + pw.close(); + + httpServletResponse.sendError(401); + } + } + + private void validateClient(OAuthTokenRequest oauthRequest) { + } + + +} diff --git a/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/ThirdPartApp.java b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/ThirdPartApp.java new file mode 100644 index 0000000..a0cf457 --- /dev/null +++ b/apache_oltu_oauth2_demo_codeScheme/src/main/java/com/oauth/code/ThirdPartApp.java @@ -0,0 +1,109 @@ +package com.oauth.code; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.oltu.oauth2.client.OAuthClient; +import org.apache.oltu.oauth2.client.URLConnectionClient; +import org.apache.oltu.oauth2.client.request.OAuthClientRequest; +import org.apache.oltu.oauth2.client.response.GitHubTokenResponse; +import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse; +import org.apache.oltu.oauth2.common.exception.OAuthProblemException; +import org.apache.oltu.oauth2.common.exception.OAuthSystemException; +import org.apache.oltu.oauth2.common.message.types.GrantType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by wuhuachuan on 16/5/9. + */ + +@RestController +public class ThirdPartApp { + + private final String oauthServer_getCode_url = "http://localhost:8080/oauthserver/get-code"; + private final String oauthServer_getToken_url = "http://localhost:8080/oauthserver/get-token"; + private final String redirect_url = "http://localhost:8080/thirdpart/token"; + private final String tokenfail_url = "http://localhost:8080/thirdpart/tokenfail"; + private final String client_id = "whc_client_id"; + private final String client_secret = "whc_client_secret"; + private final String response_type = "code"; + + /** + * 第一步调用该方法 + * 作用:请求 oauth 服务器的code,oauth服务器返回code之后,会调用 redirect_url. + * 这里的 redirect_url = http://localhost:8080/thirdpart/token , + * 所以说 oauth 服务器会调用 下面的 token 方法. + * @throws IOException + * @throws OAuthSystemException + */ + @RequestMapping(value = "/thirdpart/code",method = RequestMethod.GET) + public void code() throws IOException, OAuthSystemException { + + OAuthClientRequest request = OAuthClientRequest + .authorizationLocation(oauthServer_getCode_url) + .setClientId(client_id) + .setRedirectURI(redirect_url) + .setResponseType(response_type) + .buildQueryMessage(); + + GetMethod getMethod = new GetMethod(request.getLocationUri()); + getMethod.setFollowRedirects(true); + + HttpClient httpClient = new HttpClient(); + httpClient.executeMethod(getMethod); + } + + /** + * 该方法会被 oauth 服务器调用,同时 oauth 服务器传回来 code. + * 然后该方法紧接着会继续请求 oauth 服务器, 利用 code,拿到 token. + * @param httpServletRequest + * @param response + * @throws OAuthProblemException + * @throws OAuthSystemException + * @throws IOException + */ + @RequestMapping(value = "/thirdpart/token",method = RequestMethod.GET) + public void token(HttpServletRequest httpServletRequest, HttpServletResponse response) + throws OAuthProblemException, OAuthSystemException, IOException { + + OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(httpServletRequest); + String code = oar.getCode(); + + OAuthClientRequest request = OAuthClientRequest + .tokenLocation(oauthServer_getToken_url) + .setGrantType(GrantType.AUTHORIZATION_CODE) + .setClientId(client_id) + .setClientSecret(client_secret) + .setRedirectURI(tokenfail_url) + .setCode(code) + .buildQueryMessage(); + + OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); + + //Ps:这里之所以 有个 github 的东西,不需要管,因为从源码得知没有影响, + //关键这里需要取得 token 等信息, GitHubTokenResponse 相当于帮我们写了 + //如果我们需要自定义,那么参考这个 GitHubTokenResponse 继承相关类即可 + GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class); + + String accessToken = oAuthResponse.getAccessToken(); + Long expiresIn = oAuthResponse.getExpiresIn(); + + //这里便拿到了 token, 可以做持久化. + System.out.println("accessToken = " + accessToken); + System.out.println("expiresIn = " + expiresIn); + } + + /** + * 这里正常情况下都不会调用,但是假如上述的 拿 token 发生未知错误,则会响应该方法. + * 该方法的 url 也是由上一步 token 方法 传给了 oauth 服务器 + */ + @RequestMapping(value = "/thirdpart/tokenfail",method = RequestMethod.GET) + public void tokenfail(){ + System.out.println("get token fail!"); + } +} diff --git a/java_jdbc/src/com/jdbc/test/entity/User.java b/java_jdbc/src/com/jdbc/test/entity/User.java new file mode 100644 index 0000000..0336ab9 --- /dev/null +++ b/java_jdbc/src/com/jdbc/test/entity/User.java @@ -0,0 +1,24 @@ +package com.jdbc.test.entity; + +/** + * Created by wuhuachuan on 16/5/17. + */ + +public class User { + private String username; + private String password; + + public User(){} + public User(String username,String password){ + this.username = username; + this.password = password; + } + + @Override + public String toString() { + return "User{" + + "username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/java_jdbc/src/com/jdbc/test/main/Main.java b/java_jdbc/src/com/jdbc/test/main/Main.java new file mode 100644 index 0000000..5332ced --- /dev/null +++ b/java_jdbc/src/com/jdbc/test/main/Main.java @@ -0,0 +1,60 @@ +package com.jdbc.test.main; + +import com.jdbc.test.entity.User; +import java.sql.*; + +public class Main { + + private final static String MYSQL_DRIVER_NAME = "com.mysql.cj.jdbc.Driver"; + private final static String DEFAULT_MYSQL_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false"; + private final static String DEFAULT_MYSQL_USERNAME = "root"; + private final static String DEFAULT_MYSQL_PASSWORD = "123456"; + + private final static String DEFAULT_USER_SELECT_STATEMENT = "select * from tb_user"; + + public static void main(String[] args) throws SQLException { + //1. 加载驱动类 + try{ + Class.forName(MYSQL_DRIVER_NAME) ; + }catch(ClassNotFoundException e){ + e.printStackTrace() ; + } + + //2. 提供JDBC连接的URL,USERNAME,PASSWORD + + + //3. 创建数据库的连接 + Connection connection = null; + ResultSet resultSet = null; + try{ + connection = DriverManager.getConnection(DEFAULT_MYSQL_URL,DEFAULT_MYSQL_USERNAME,DEFAULT_MYSQL_PASSWORD); + + //4. 创建 Statement对象 + Statement statement = connection.createStatement(); + + //5. 执行SQL 语句 + resultSet = statement.executeQuery(DEFAULT_USER_SELECT_STATEMENT); + + //6. 处理结果 + while(resultSet.next() == true){ + String username = resultSet.getString("username"); + String password = resultSet.getString("password"); + + User user = new User(username,password); + System.out.println("user = " + user); + } + + // 7. 关闭链接 + resultSet.close(); + connection.close(); + + } finally { + if(resultSet != null ){ + resultSet.close(); + } + if(connection != null ){ + connection.close(); + } + } + } +} diff --git a/java_jdbc/src/resources/mysql-connector-java-6.0.2.jar b/java_jdbc/src/resources/mysql-connector-java-6.0.2.jar new file mode 100644 index 0000000..211af14 Binary files /dev/null and b/java_jdbc/src/resources/mysql-connector-java-6.0.2.jar differ diff --git a/java_jdbc/src/resources/tb_user.sql b/java_jdbc/src/resources/tb_user.sql new file mode 100644 index 0000000..9601994 --- /dev/null +++ b/java_jdbc/src/resources/tb_user.sql @@ -0,0 +1,52 @@ +-- MySQL dump 10.13 Distrib 5.6.22, for osx10.8 (x86_64) +-- +-- Host: 127.0.0.1 Database: test +-- ------------------------------------------------------ +-- Server version 5.6.20 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `tb_user` +-- + +DROP TABLE IF EXISTS `tb_user`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tb_user` ( + `username` varchar(255) NOT NULL, + `password` varchar(255) DEFAULT NULL, + PRIMARY KEY (`username`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `tb_user` +-- + +LOCK TABLES `tb_user` WRITE; +/*!40000 ALTER TABLE `tb_user` DISABLE KEYS */; +INSERT INTO `tb_user` VALUES ('admin','admin'); +INSERT INTO `tb_user` VALUES ('admin1','admin1'); +/*!40000 ALTER TABLE `tb_user` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2016年05月17日 23:41:48 diff --git a/quartz/pom.xml b/quartz/pom.xml new file mode 100644 index 0000000..cea3679 --- /dev/null +++ b/quartz/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.quartz.test + com.quartz.test + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + UTF-8 + com.quartz.Boot + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + 1.16.6 + + + org.quartz-scheduler + quartz + 2.2.2 + + + org.springframework + spring-context-support + 4.2.5.RELEASE + + + org.springframework + spring-tx + 4.2.5.RELEASE + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/quartz/src/main/java/com/quartz/Boot.java b/quartz/src/main/java/com/quartz/Boot.java new file mode 100644 index 0000000..cd7637f --- /dev/null +++ b/quartz/src/main/java/com/quartz/Boot.java @@ -0,0 +1,18 @@ +package com.quartz; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * Created by wuhuachuan on 16/6/9. + */ +@EnableAutoConfiguration +@ComponentScan +@Configuration +public class Boot { + public static void main(String[] args) throws Exception { + SpringApplication.run(Boot.class, args); + } +} diff --git a/quartz/src/main/java/com/quartz/MyJob.java b/quartz/src/main/java/com/quartz/MyJob.java new file mode 100644 index 0000000..ee6203e --- /dev/null +++ b/quartz/src/main/java/com/quartz/MyJob.java @@ -0,0 +1,24 @@ +package com.quartz; + +import lombok.extern.slf4j.Slf4j; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.quartz.SchedulerException; + +/** + * Created by wuhuachuan on 16/6/9. + */ +@Slf4j +public class MyJob implements Job { + @Override + public void execute(JobExecutionContext context) throws JobExecutionException { + log.info("Do the job!"); + try { + log.info("job scheduler = {}.",context.getScheduler().getSchedulerName()); + } catch (SchedulerException e) { + e.printStackTrace(); + } + log.info("job trigger = {}.",context.getTrigger()); + } +} diff --git a/quartz/src/main/java/com/quartz/SchedulerJob.java b/quartz/src/main/java/com/quartz/SchedulerJob.java new file mode 100644 index 0000000..c0b1834 --- /dev/null +++ b/quartz/src/main/java/com/quartz/SchedulerJob.java @@ -0,0 +1,58 @@ +package com.quartz; + +import lombok.extern.slf4j.Slf4j; +import org.quartz.CronTrigger; +import org.quartz.JobDetail; +import org.quartz.Trigger; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.quartz.CronTriggerFactoryBean; +import org.springframework.scheduling.quartz.JobDetailFactoryBean; +import org.springframework.scheduling.quartz.SchedulerFactoryBean; +import java.util.List; + +/** + * Created by wuhuachuan on 16/6/9. + * + * 该类主要用于单机版本 + */ + +@Slf4j +//@Configuration +public class SchedulerJob { + + private final static String CRON_EXPRESSION = "*/3 * * * * ?"; + + @Bean + public JobDetailFactoryBean createJobDetail(){ + JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();jobDetailFactoryBean.setName("myJobName"); + jobDetailFactoryBean.setGroup("myJobGroup"); + + jobDetailFactoryBean.setJobClass(MyJob.class); + + return jobDetailFactoryBean; + } + + @Bean + protected CronTriggerFactoryBean createTrigger(JobDetail jobDetail) { + CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean(); + cronTriggerFactoryBean.setName("myTriggerName"); + cronTriggerFactoryBean.setGroup("myTriggerGroup"); + + cronTriggerFactoryBean.setCronExpression(CRON_EXPRESSION); + cronTriggerFactoryBean.setJobDetail(jobDetail); + cronTriggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY); + + return cronTriggerFactoryBean; + } + + @Bean + public SchedulerFactoryBean schedulerFactoryBean(List
triggers){ + SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); + schedulerFactoryBean.setSchedulerName("myScheduleName"); + + schedulerFactoryBean.setTriggers(triggers.toArray(new Trigger[0])); + + return schedulerFactoryBean; + } +} diff --git a/quartz/src/main/java/com/quartz/SchedulerJobCluster.java b/quartz/src/main/java/com/quartz/SchedulerJobCluster.java new file mode 100644 index 0000000..e551e66 --- /dev/null +++ b/quartz/src/main/java/com/quartz/SchedulerJobCluster.java @@ -0,0 +1,77 @@ +package com.quartz; + +import lombok.extern.slf4j.Slf4j; +import org.quartz.CronTrigger; +import org.quartz.JobDetail; +import org.quartz.Trigger; +import org.springframework.beans.factory.config.PropertiesFactoryBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.scheduling.quartz.CronTriggerFactoryBean; +import org.springframework.scheduling.quartz.JobDetailFactoryBean; +import org.springframework.scheduling.quartz.SchedulerFactoryBean; + +import javax.sql.DataSource; +import java.io.IOException; +import java.util.List; +import java.util.Properties; + +/** + * Created by wuhuachuan on 16/6/11. + * + * 该类主要用于集群版本 + */ + +@Slf4j +@Configuration +public class SchedulerJobCluster { + + private final static String CRON_EXPRESSION = "*/5 * * * * ?"; + + @Bean + public JobDetailFactoryBean createJobDetail(){ + JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();jobDetailFactoryBean.setName("myJobName"); + jobDetailFactoryBean.setGroup("myJobGroup"); + + jobDetailFactoryBean.setJobClass(MyJob.class); + + return jobDetailFactoryBean; + } + + @Bean + protected CronTriggerFactoryBean createTrigger(JobDetail jobDetail) { + CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean(); + cronTriggerFactoryBean.setName("myTriggerName"); + cronTriggerFactoryBean.setGroup("myTriggerGroup"); + + cronTriggerFactoryBean.setCronExpression(CRON_EXPRESSION); + cronTriggerFactoryBean.setJobDetail(jobDetail); + cronTriggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY); + + return cronTriggerFactoryBean; + } + + @Bean + public SchedulerFactoryBean schedulerFactoryBean(List
triggers, DataSource dataSource){ + SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); + + schedulerFactoryBean.setTriggers(triggers.toArray(new Trigger[0])); + schedulerFactoryBean.setQuartzProperties(quartzProperties()); + schedulerFactoryBean.setDataSource(dataSource); + + return schedulerFactoryBean; + } + + private Properties quartzProperties(){ + PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); + propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); + try { + propertiesFactoryBean.afterPropertiesSet(); + return propertiesFactoryBean.getObject(); + } catch (IOException e) { + log.error("read quartz.properties file error: {}", e.getMessage()); + } + return null; + } +} diff --git a/quartz/src/main/resources/application.yml b/quartz/src/main/resources/application.yml new file mode 100644 index 0000000..c66ce7a --- /dev/null +++ b/quartz/src/main/resources/application.yml @@ -0,0 +1,22 @@ +spring: + datasource: + test-on-borrow: true + validationQuery: SELECT 1 + driverClassName: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 + password: 123456 + username: root + jpa: + hibernate: + ddl-auto: update + naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy + properties: + hibernate.globally_quoted_identifiers: true + hibernate.show_sql: false + hibernate.format_sql: true + + + + + + diff --git a/quartz/src/main/resources/quartz.properties b/quartz/src/main/resources/quartz.properties new file mode 100644 index 0000000..beb5abf --- /dev/null +++ b/quartz/src/main/resources/quartz.properties @@ -0,0 +1,26 @@ +#============================================================================ +# Configure Main Scheduler Properties +#============================================================================ +org.quartz.scheduler.instanceName = whcClusteredScheduler +org.quartz.scheduler.instanceId = AUTO + +#============================================================================ +# Configure ThreadPool +#============================================================================ + +org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool +org.quartz.threadPool.threadCount = 25 +org.quartz.threadPool.threadPriority = 5 + + +#============================================================================ +# Configure JobStore +#============================================================================ +org.quartz.jobStore.misfireThreshold = 60000 +org.quartz.jobStore.class = org.springframework.scheduling.quartz.LocalDataSourceJobStore +org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate +org.quartz.jobStore.useProperties = true +org.quartz.jobStore.tablePrefix = QRTZ_ +org.quartz.jobStore.isClustered = true +org.quartz.jobStore.clusterCheckinInterval = 1000 + diff --git a/rabbitmq/pom.xml b/rabbitmq/pom.xml new file mode 100644 index 0000000..d22db05 --- /dev/null +++ b/rabbitmq/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + com.rabbitmq.test + com.rabbitmq.test + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.amqp + spring-amqp + 1.5.3.RELEASE + + + org.springframework.amqp + spring-rabbit + 1.5.3.RELEASE + + + org.projectlombok + lombok + 1.16.6 + + + + \ No newline at end of file diff --git a/rabbitmq/src/main/java/test/boot/Boot.java b/rabbitmq/src/main/java/test/boot/Boot.java new file mode 100644 index 0000000..33b9b29 --- /dev/null +++ b/rabbitmq/src/main/java/test/boot/Boot.java @@ -0,0 +1,17 @@ +package test.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +/** + * Created by wuhuachuan on 16/3/3. + */ + +@EnableAutoConfiguration +@ComponentScan(basePackages="test") +public class Boot { + public static void main(String[] args) throws Exception { + SpringApplication.run(Boot.class, args); + } +} diff --git a/rabbitmq/src/main/java/test/config/RabbitMQConfig.java b/rabbitmq/src/main/java/test/config/RabbitMQConfig.java new file mode 100644 index 0000000..f071377 --- /dev/null +++ b/rabbitmq/src/main/java/test/config/RabbitMQConfig.java @@ -0,0 +1,69 @@ +package test.config; + +import lombok.Data; +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.core.TopicExchange; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; +import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import test.receiver.Receiver; + + +/** + * Created by wuhuachuan on 16/3/28. + */ +@Configuration +@Data +public class RabbitMQConfig { + + final static String queueName = "spring-boot"; + + @Bean + Queue queue() { + return new Queue(queueName, false); + } + + @Bean + TopicExchange exchange() { + return new TopicExchange("spring-boot-exchange"); + } + + @Bean + Binding binding(Queue queue, TopicExchange exchange) { + return BindingBuilder.bind(queue).to(exchange).with(queueName); + } + + @Bean + ConnectionFactory connectionFactory() { + CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672); + connectionFactory.setUsername("guest"); + connectionFactory.setPassword("guest"); + connectionFactory.setPublisherConfirms(true); // enable confirm mode + return connectionFactory; + } + + @Bean + SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { + + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); + container.setConnectionFactory(connectionFactory); + container.setQueueNames(queueName); + container.setMessageListener(listenerAdapter); + return container; + } + + @Bean + Receiver receiver() { + return new Receiver(); + } + + @Bean + MessageListenerAdapter listenerAdapter(Receiver receiver) { + return new MessageListenerAdapter(receiver, "receiveMessage"); + } +} diff --git a/rabbitmq/src/main/java/test/producer/Producer.java b/rabbitmq/src/main/java/test/producer/Producer.java new file mode 100644 index 0000000..4190a69 --- /dev/null +++ b/rabbitmq/src/main/java/test/producer/Producer.java @@ -0,0 +1,26 @@ +package test.producer; + +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by wuhuachuan on 16/3/28. + */ + +@RestController +public class Producer { + + @Autowired + RabbitTemplate rabbitTemplate; + + @RequestMapping(value = "/test/{abc}",method = RequestMethod.GET) + public String test(@PathVariable(value = "abc") String abc){ + + rabbitTemplate.convertAndSend("spring-boot-exchange","spring-boot", abc + " from RabbitMQ!"); + return "abc"; + } +} diff --git a/rabbitmq/src/main/java/test/receiver/Receiver.java b/rabbitmq/src/main/java/test/receiver/Receiver.java new file mode 100644 index 0000000..7d33bea --- /dev/null +++ b/rabbitmq/src/main/java/test/receiver/Receiver.java @@ -0,0 +1,10 @@ +package test.receiver; + +/** + * Created by wuhuachuan on 16/3/28. + */ +public class Receiver { + public void receiveMessage(String message) { + System.out.println("Received <" + message + ">"); + } +} diff --git a/rabbitmq/src/main/resources/application.yml b/rabbitmq/src/main/resources/application.yml new file mode 100644 index 0000000..1c6982f --- /dev/null +++ b/rabbitmq/src/main/resources/application.yml @@ -0,0 +1,7 @@ +#spring: +# autoconfigure: +# excludes: HibernateJpaAutoConfiguration + + + + diff --git a/rabbitmq_using_springboot_annotation/pom.xml b/rabbitmq_using_springboot_annotation/pom.xml new file mode 100644 index 0000000..d22db05 --- /dev/null +++ b/rabbitmq_using_springboot_annotation/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + com.rabbitmq.test + com.rabbitmq.test + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.amqp + spring-amqp + 1.5.3.RELEASE + + + org.springframework.amqp + spring-rabbit + 1.5.3.RELEASE + + + org.projectlombok + lombok + 1.16.6 + + + + \ No newline at end of file diff --git a/rabbitmq_using_springboot_annotation/src/main/java/test/boot/Boot.java b/rabbitmq_using_springboot_annotation/src/main/java/test/boot/Boot.java new file mode 100644 index 0000000..33b9b29 --- /dev/null +++ b/rabbitmq_using_springboot_annotation/src/main/java/test/boot/Boot.java @@ -0,0 +1,17 @@ +package test.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +/** + * Created by wuhuachuan on 16/3/3. + */ + +@EnableAutoConfiguration +@ComponentScan(basePackages="test") +public class Boot { + public static void main(String[] args) throws Exception { + SpringApplication.run(Boot.class, args); + } +} diff --git a/rabbitmq_using_springboot_annotation/src/main/java/test/config/RabbitMQConfig.java b/rabbitmq_using_springboot_annotation/src/main/java/test/config/RabbitMQConfig.java new file mode 100644 index 0000000..bf7c26e --- /dev/null +++ b/rabbitmq_using_springboot_annotation/src/main/java/test/config/RabbitMQConfig.java @@ -0,0 +1,27 @@ +package test.config; + +import lombok.Data; +import org.springframework.amqp.rabbit.annotation.EnableRabbit; +import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +/** + * Created by wuhuachuan on 16/3/28. + */ +@EnableRabbit +@Configuration +@Data +public class RabbitMQConfig { + + @Bean(name = "myConnectionFactory") + SimpleRabbitListenerContainerFactory createSimpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory) { + + SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + + return factory; + } +} diff --git a/rabbitmq_using_springboot_annotation/src/main/java/test/producer/Producer.java b/rabbitmq_using_springboot_annotation/src/main/java/test/producer/Producer.java new file mode 100644 index 0000000..246ae75 --- /dev/null +++ b/rabbitmq_using_springboot_annotation/src/main/java/test/producer/Producer.java @@ -0,0 +1,25 @@ +package test.producer; + +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by wuhuachuan on 16/3/28. + */ + +@RestController +public class Producer { + + @Autowired + RabbitTemplate rabbitTemplate; + + @RequestMapping(value = "/test/{abc}",method = RequestMethod.GET) + public String test(@PathVariable(value = "abc") String abc){ + rabbitTemplate.convertAndSend("testDirectExchange","key1", abc + " from RabbitMQ!"); + return "abc"; + } +} diff --git a/rabbitmq_using_springboot_annotation/src/main/java/test/receiver/Receiver.java b/rabbitmq_using_springboot_annotation/src/main/java/test/receiver/Receiver.java new file mode 100644 index 0000000..1ebf9fe --- /dev/null +++ b/rabbitmq_using_springboot_annotation/src/main/java/test/receiver/Receiver.java @@ -0,0 +1,24 @@ +package test.receiver; + +import org.springframework.amqp.core.ExchangeTypes; +import org.springframework.amqp.rabbit.annotation.*; +import org.springframework.stereotype.Component; + +/** + * Created by wuhuachuan on 16/3/28. + */ + +@RabbitListener( + containerFactory = "myConnectionFactory", + bindings = @QueueBinding( + value = @Queue(value = "testDirectQueue1",durable = "true"), + exchange = @Exchange(value = "testDirectExchange",type = ExchangeTypes.DIRECT), + key = "key1") +) +@Component +public class Receiver { + @RabbitHandler + public void receiveMessage(String message) { + System.out.println("Received <" + message + ">"); + } +} diff --git a/rabbitmq_using_springboot_annotation/src/main/resources/application.yml b/rabbitmq_using_springboot_annotation/src/main/resources/application.yml new file mode 100644 index 0000000..e69de29 diff --git a/spring_oauth2_demo_passwordScheme/pom.xml b/spring_oauth2_demo_passwordScheme/pom.xml new file mode 100644 index 0000000..7ae1b3b --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.springoauth.code + com.springoauth.code + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.springframework.boot + spring-boot-starter-security + + + org.projectlombok + lombok + 1.16.6 + + + org.springframework.security.oauth + spring-security-oauth2 + 2.0.9.RELEASE + + + \ No newline at end of file diff --git a/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/boot/Boot.java b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/boot/Boot.java new file mode 100644 index 0000000..ac32d39 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/boot/Boot.java @@ -0,0 +1,23 @@ +package com.springoauth.code.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.orm.jpa.EntityScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +/** + * Created by wuhuachuan on 16/5/9. + */ +@Configuration +@EnableAutoConfiguration +@ComponentScan(basePackages = { "com.springoauth.code.*" }) +@EntityScan(basePackages = { "com.springoauth.code.*" }) +@EnableJpaRepositories(basePackages = { "com.springoauth.code.*" }) +public class Boot { + public static void main(String[] args) throws Exception { + SpringApplication.run(Boot.class, args); + } +} diff --git a/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/MyPasswordManager.java b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/MyPasswordManager.java new file mode 100644 index 0000000..5c10f68 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/MyPasswordManager.java @@ -0,0 +1,32 @@ +package com.springoauth.code.config; + +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * Created by wuhuachuan on 16/5/16. + */ + +@Component(value = "myPasswordManager") +public class MyPasswordManager implements AuthenticationManager{ + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + + ArrayList authorities = new ArrayList(); + authorities.add(new SimpleGrantedAuthority("whc1")); + + UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = + new UsernamePasswordAuthenticationToken("whc_client_id1","whc_client_secret1",authorities); + + return usernamePasswordAuthenticationToken; + } +} diff --git a/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/OAuth2Config.java b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/OAuth2Config.java new file mode 100644 index 0000000..71d82fb --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/OAuth2Config.java @@ -0,0 +1,41 @@ +package com.springoauth.code.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; +import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; +import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; + +import javax.sql.DataSource; + +/** + * Created by wuhuachuan on 16/5/12. + */ + +@Configuration +@EnableAuthorizationServer +@EnableResourceServer +public class OAuth2Config extends AuthorizationServerConfigurerAdapter { + + @Autowired + private DataSource dataSource; + + @Autowired + private MyPasswordManager myPasswordManager; + + @Override + public void configure(AuthorizationServerEndpointsConfigurer endpoints) + throws Exception { + endpoints.tokenStore(new JdbcTokenStore(dataSource)); + endpoints.authenticationManager(myPasswordManager); + } + + @Override + public void configure(ClientDetailsServiceConfigurer clients) throws Exception { + clients.withClientDetails(new JdbcClientDetailsService(dataSource)); + } +} diff --git a/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/SpringSecurityConfig.java b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/SpringSecurityConfig.java new file mode 100644 index 0000000..555a4f7 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/config/SpringSecurityConfig.java @@ -0,0 +1,13 @@ +package com.springoauth.code.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; + +/** + * Created by wuhuachuan on 16/5/16. + */ + +@EnableWebSecurity +@Configuration +public class SpringSecurityConfig { +} diff --git a/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/controller/TestController.java b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/controller/TestController.java new file mode 100644 index 0000000..b036948 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/java/com/springoauth/code/controller/TestController.java @@ -0,0 +1,17 @@ +package com.springoauth.code.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by wuhuachuan on 16/5/11. + */ + +@RestController +public class TestController { + + @RequestMapping(value = "/test") + public void test(){ + System.out.println("test!"); + } +} diff --git a/spring_oauth2_demo_passwordScheme/src/main/resources/application.yml b/spring_oauth2_demo_passwordScheme/src/main/resources/application.yml new file mode 100644 index 0000000..71fb7e6 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/resources/application.yml @@ -0,0 +1,16 @@ +spring: + datasource: + test-on-borrow: true + validationQuery: SELECT 1 + driverClassName: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/springoauth?useUnicode=true&characterEncoding=UTF-8 + password: 123456 + username: root + jpa: + hibernate: + ddl-auto: update + naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy + properties: + hibernate.globally_quoted_identifiers: true + hibernate.show_sql: false + hibernate.format_sql: true \ No newline at end of file diff --git a/spring_oauth2_demo_passwordScheme/src/main/resources/oauth.sql b/spring_oauth2_demo_passwordScheme/src/main/resources/oauth.sql new file mode 100644 index 0000000..26fd351 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/resources/oauth.sql @@ -0,0 +1,52 @@ +create table oauth_client_details ( + client_id VARCHAR(255) PRIMARY KEY, + resource_ids VARCHAR(255), + client_secret VARCHAR(255), + scope VARCHAR(255), + authorized_grant_types VARCHAR(255), + web_server_redirect_uri VARCHAR(255), + authorities VARCHAR(255), + access_token_validity INTEGER, + refresh_token_validity INTEGER, + additional_information VARCHAR(4096), + autoapprove varchar(255) +); + +create table oauth_client_token ( + token_id VARCHAR(255), + token BLOB, + authentication_id VARCHAR(255), + user_name VARCHAR(255), + client_id VARCHAR(255) +); + +create table oauth_access_token ( + token_id VARCHAR(255), + token BLOB, + authentication_id VARCHAR(255), + user_name VARCHAR(255), + client_id VARCHAR(255), + authentication BLOB, + refresh_token VARCHAR(255) +); + +create table oauth_refresh_token ( + token_id VARCHAR(255), + token BLOB, + authentication BLOB + +); + +create table oauth_code ( + code VARCHAR(255), authentication BLOB +); + +create table oauth_approvals ( + userId VARCHAR(256), + clientId VARCHAR(256), + scope VARCHAR(256), + status VARCHAR(10), + expiresAt TIMESTAMP, + lastModifiedAt TIMESTAMP +); + diff --git a/spring_oauth2_demo_passwordScheme/src/main/resources/oauth_data_init.sql b/spring_oauth2_demo_passwordScheme/src/main/resources/oauth_data_init.sql new file mode 100644 index 0000000..71e2bb9 --- /dev/null +++ b/spring_oauth2_demo_passwordScheme/src/main/resources/oauth_data_init.sql @@ -0,0 +1,39 @@ +-- MySQL dump 10.13 Distrib 5.6.22, for osx10.8 (x86_64) +-- +-- Host: 127.0.0.1 Database: springoauth +-- ------------------------------------------------------ +-- Server version 5.6.20 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Dumping data for table `oauth_client_details` +-- + +LOCK TABLES `oauth_client_details` WRITE; +/*!40000 ALTER TABLE `oauth_client_details` DISABLE KEYS */; +INSERT INTO `oauth_client_details` VALUES ('whc_client_id','','whc_client_secret','whc','client_credentials','http://localhost:8080/test','',86400,172800,'{}','whc'); +INSERT INTO `oauth_client_details` VALUES ('whc_client_id1','','whc_client_secret1','whc1','password','http://localhost:8080/test','',86400,172800,'{}','whc1'); + +/*!40000 ALTER TABLE `oauth_client_details` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2016年05月16日 15:57:38 diff --git a/spring_security_dmeo/pom.xml b/spring_security_dmeo/pom.xml new file mode 100644 index 0000000..fa01b64 --- /dev/null +++ b/spring_security_dmeo/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.springoauth2.demo + com.springoauth2.demo + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.springframework.boot + spring-boot-starter-security + + + org.projectlombok + lombok + 1.16.6 + + + + \ No newline at end of file diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/boot/Boot.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/boot/Boot.java new file mode 100644 index 0000000..16dfd4b --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/boot/Boot.java @@ -0,0 +1,22 @@ +package com.springoauth2.demo.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.orm.jpa.EntityScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +/** + * Created by wuhuachuan on 16/5/9. + */ +@Configuration +@EnableAutoConfiguration +@ComponentScan(basePackages = { "com.springoauth2.demo.*" }) +@EntityScan(basePackages = { "com.springoauth2.demo.*" }) +@EnableJpaRepositories(basePackages = { "com.springoauth2.demo.*" }) +public class Boot { + public static void main(String[] args) throws Exception { + SpringApplication.run(Boot.class, args); + } +} diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/config/MyUserDetailService.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/config/MyUserDetailService.java new file mode 100644 index 0000000..b0667dc --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/config/MyUserDetailService.java @@ -0,0 +1,54 @@ +package com.springoauth2.demo.config; + +import com.springoauth2.demo.entity.User; +import com.springoauth2.demo.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter; +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +/** + * Created by wuhuachuan on 16/5/12. + */ +@Configuration +public class MyUserDetailService extends GlobalAuthenticationConfigurerAdapter { + + @Autowired + private UserService userService; + + @Override + public void init(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService()); + } + + @Bean + public UserDetailsService userDetailsService() { + return new UserDetailsService() { + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + + User user = userService.findByUsername(username); + + if(user != null){ + return new org.springframework.security.core.userdetails.User( + user.getUsername(), + user.getPassword(), + true, + true, + true, + true, + AuthorityUtils.createAuthorityList("USER") + ); + } else { + throw new UsernameNotFoundException("could not find the user '" + + username + "'"); + } + } + }; + } +} diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/config/SpringSecurityConfig.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/config/SpringSecurityConfig.java new file mode 100644 index 0000000..9036736 --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/config/SpringSecurityConfig.java @@ -0,0 +1,25 @@ +package com.springoauth2.demo.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * Created by wuhuachuan on 16/5/12. + */ +@EnableWebSecurity +@Configuration +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .anyRequest() + .fullyAuthenticated() + .and() + .httpBasic() + .and() + .csrf().disable(); + } +} \ No newline at end of file diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/controller/TestController.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/controller/TestController.java new file mode 100644 index 0000000..498e5c5 --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/controller/TestController.java @@ -0,0 +1,17 @@ +package com.springoauth2.demo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by wuhuachuan on 16/5/11. + */ + +@RestController +public class TestController { + + @RequestMapping(value = "/test") + public void test(){ + System.out.println("test!"); + } +} diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/dao/UserDao.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/dao/UserDao.java new file mode 100644 index 0000000..34b7261 --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/dao/UserDao.java @@ -0,0 +1,15 @@ +package com.springoauth2.demo.dao; + +import com.springoauth2.demo.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +/** + * Created by wuhuachuan on 16/5/11. + */ +public interface UserDao extends JpaRepository{ + + @Query("select user from User user where username = :username and enable = true") + User findByUsername(@Param("username") String username); +} diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/entity/User.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/entity/User.java new file mode 100644 index 0000000..25a2472 --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/entity/User.java @@ -0,0 +1,22 @@ +package com.springoauth2.demo.entity; + +import lombok.Data; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * Created by wuhuachuan on 16/5/11. + */ + +@Entity +@Table(name = "tb_user") +@Data +public class User { + @Id + private String id; + private String username; + private String password; + private boolean enable = true; +} diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/service/UserService.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/service/UserService.java new file mode 100644 index 0000000..6a4245f --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/service/UserService.java @@ -0,0 +1,11 @@ +package com.springoauth2.demo.service; + +import com.springoauth2.demo.entity.User; + +/** + * Created by wuhuachuan on 16/5/11. + */ +public interface UserService { + + public User findByUsername(String username); +} diff --git a/spring_security_dmeo/src/main/java/com/springoauth2/demo/serviceImpl/UserServiceImpl.java b/spring_security_dmeo/src/main/java/com/springoauth2/demo/serviceImpl/UserServiceImpl.java new file mode 100644 index 0000000..deaeafd --- /dev/null +++ b/spring_security_dmeo/src/main/java/com/springoauth2/demo/serviceImpl/UserServiceImpl.java @@ -0,0 +1,23 @@ +package com.springoauth2.demo.serviceImpl; + +import com.springoauth2.demo.dao.UserDao; +import com.springoauth2.demo.entity.User; +import com.springoauth2.demo.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * Created by wuhuachuan on 16/5/11. + */ + +@Service +public class UserServiceImpl implements UserService{ + + @Autowired + private UserDao userDao; + + @Override + public User findByUsername(String username) { + return userDao.findByUsername(username); + } +} diff --git a/spring_security_dmeo/src/main/resources/application.yml b/spring_security_dmeo/src/main/resources/application.yml new file mode 100644 index 0000000..d8c8f37 --- /dev/null +++ b/spring_security_dmeo/src/main/resources/application.yml @@ -0,0 +1,17 @@ +spring: + datasource: + test-on-borrow: true + validationQuery: SELECT 1 + driverClassName: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/springoauth?useUnicode=true&characterEncoding=UTF-8 + password: 123456 + username: root + jpa: + hibernate: + ddl-auto: update + naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy + properties: + hibernate.globally_quoted_identifiers: true + hibernate.show_sql: false + hibernate.format_sql: true +

AltStyle によって変換されたページ (->オリジナル) /