diff --git a/pom.xml b/pom.xml
index 8f055d2..1825179 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,12 +33,12 @@
org.springframework.boot
spring-boot-starter-data-jpa
-
+
org.springframework.boot
spring-boot-starter-data-redis
-
-
+
+
org.springframework.boot
spring-boot-starter-web
@@ -49,26 +49,56 @@
mysql-connector-java
runtime
-
+
- org.springframework.boot
- spring-boot-starter-cache
+ org.springframework.boot
+ spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-thymeleaf
-
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
org.springframework.boot
spring-boot-starter-test
test
+
+
+ com.vpgame.vendor
+ core
+ 1.0.2-SNAPSHOT
+
+
+
+
+ org.apache.zookeeper
+ zookeeper
+ 3.4.8
+
+
+
+
+ true
+
+ ${project.basedir}/src/main/resources
+
+ *.properties
+
+
+
+
org.springframework.boot
@@ -77,5 +107,14 @@
+
+
+
+ local
+
+ 127.0.0.1:2181
+
+
+
diff --git a/src/main/java/com/springboot/demo/config/CustomProperty.java b/src/main/java/com/springboot/demo/config/CustomProperty.java
new file mode 100644
index 0000000..a4239e2
--- /dev/null
+++ b/src/main/java/com/springboot/demo/config/CustomProperty.java
@@ -0,0 +1,19 @@
+package com.springboot.demo.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@ConfigurationProperties(prefix = "com.springboot.demo")
+public class CustomProperty {
+ private String zookeeperUrl;
+
+ public String getZookeeperUrl() {
+ return zookeeperUrl;
+ }
+
+ public void setZookeeperUrl(String zookeeperUrl) {
+ this.zookeeperUrl = zookeeperUrl;
+ }
+
+}
diff --git a/src/main/java/com/springboot/demo/config/DataSourceConfig.java b/src/main/java/com/springboot/demo/config/DataSourceConfig.java
index 3550328..1f38c28 100644
--- a/src/main/java/com/springboot/demo/config/DataSourceConfig.java
+++ b/src/main/java/com/springboot/demo/config/DataSourceConfig.java
@@ -1,5 +1,9 @@
package com.springboot.demo.config;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -9,14 +13,35 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
+import com.vpgame.core.config.MysqlDbConfig;
+import com.vpgame.core.utils.ZkConfigClient;
+
@Configuration
public class DataSourceConfig {
+ @Resource
+ private CustomProperty customProperty;
+
@Bean(name="primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource(){
- return (DataSource) DataSourceBuilder.create().build();
+ //这里的数据我想直接从zookeeper中的节点中获取,这里我使用峻峰已经封装好的zkclient类来链接本地的zk
+ //这里获取一个新的zk客户端连接zk
+ ZkConfigClient zkConfigClient = new ZkConfigClient(customProperty.getZookeeperUrl());
+ //下面通过ZkConfigClient中的getMysqlConfig
+ MysqlDbConfig mysqlConfig = zkConfigClient.getMysqlDbConfig("springboot");
+
+ //下面的配置是什么我暂时还不知道
+ Map dbUriParam = new HashMap<>();
+ dbUriParam.put("zeroDateTimeBehavior", "convertToNull");
+ //下面开始生成相应的datasource
+ return (DataSource) DataSourceBuilder.create()
+ //这里创建DataSource时,可以在这里面加入获取的数据库的url,username,password等配置数据
+ .url(https://github.com/MysqlDbUtil.generateDbUrl(mysqlConfig,dbUriParam))
+ .username(mysqlConfig.getUsername())
+ .password(mysqlConfig.getPassword())
+ .build();
}
@@ -25,7 +50,7 @@ public DataSource primaryDataSource(){
@Primary
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource(){
- return (DataSource) DataSourceBuilder.create().build();
+ return (DataSource) DataSourceBuilder.create()
+ .build();
}
-
}
diff --git a/src/main/java/com/springboot/demo/config/GenerateZKNodeAndData.java b/src/main/java/com/springboot/demo/config/GenerateZKNodeAndData.java
new file mode 100644
index 0000000..5a6f364
--- /dev/null
+++ b/src/main/java/com/springboot/demo/config/GenerateZKNodeAndData.java
@@ -0,0 +1,145 @@
+//package com.springboot.demo.config;
+//
+//import java.io.IOException;
+//import java.util.List;
+//import java.util.concurrent.CountDownLatch;
+//
+//import org.apache.zookeeper.CreateMode;
+//import org.apache.zookeeper.KeeperException;
+//import org.apache.zookeeper.WatchedEvent;
+//import org.apache.zookeeper.Watcher;
+//import org.apache.zookeeper.Watcher.Event.KeeperState;
+//import org.apache.zookeeper.ZooDefs.Ids;
+//import org.apache.zookeeper.ZooKeeper;
+//import org.apache.zookeeper.data.Stat;
+//
+///**
+// * ]
+// *
+// * @author getChildren(String path) throws KeeperException, InterruptedException {
+// return this.zookeeper.getChildren(path, false);
+// }
+//
+// public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException {
+// return this.zookeeper.setData(path, data, version);
+// }
+//
+// /**
+// * 根据路径获取节点数据
+// *
+// * @param path
+// * @return
+// * @throws KeeperException
+// * @throws InterruptedException
+// */
+// public byte[] getData(String path) throws KeeperException, InterruptedException {
+// return this.zookeeper.getData(path, false, null);
+// }
+//
+// /**
+// * 删除节点
+// *
+// * @param path
+// * @param version
+// * @throws InterruptedException
+// * @throws KeeperException
+// */
+// public void deleteNode(String path, int version) throws InterruptedException, KeeperException {
+// this.zookeeper.delete(path, version);
+// }
+//
+// /**
+// * 关闭zookeeper连接
+// *
+// * @throws InterruptedException
+// */
+// public void closeConnect() throws InterruptedException {
+// if (null != zookeeper) {
+// zookeeper.close();
+// }
+// }
+//
+// // 下面是定义主方法,然后运行该类去生成相应的ZKnode和相应的数据
+// public static void main(String args[]) throws IOException, InterruptedException, KeeperException {
+// // 建立一个客户端连接ZK
+// GenerateZKNodeAndData client = new GenerateZKNodeAndData();
+// String host = "localhost:2181";
+//
+// // 下面开始连接ZK
+// client.connectZookeeper(host);
+//
+// // 连接好了以后现在开始创建节点和数据,这里的数据我在这里先定义好,这里给出的接口里必须放byte类型的数组,
+// //这里创建一次过后,就不要在创建了,否则会报异常,所以这里我将创建节点的代码注释了
+// String str = "{\"username\":\"lizhen\",\"password\":\"lizhen\",\"host\":\"127.0.0.1\",\"dbname\":\"springboot\",\"port\":3306}";
+// byte[] data = str.getBytes();
+// //String isCreate = client.createNode("/config/mysqldb/springboot", data);
+// //因为之前弄的数据库密码弄成lizhen1992815了,正确是lizhen,所以这里需要将节点的数据set一下,经过测试,目前数据已经修改过来了,现在将更新数据的代码注释掉,否则每更新一次对应的dataversion就会增加一次
+//// Stat stat = client.setData("/config/mysqldb/springboot", data, 0);
+//// // 这里返回节点的路径
+//// System.out.println(stat);
+//
+// // 下面是从zk中获取数据
+// byte[] mydata = client.getData("/config/mysqldb/springboot");
+// String result = new String(mydata);
+// System.out.println(result);
+// }
+//}
diff --git a/src/main/java/com/springboot/demo/config/MysqlDbUtil.java b/src/main/java/com/springboot/demo/config/MysqlDbUtil.java
new file mode 100644
index 0000000..ab2a05b
--- /dev/null
+++ b/src/main/java/com/springboot/demo/config/MysqlDbUtil.java
@@ -0,0 +1,43 @@
+package com.springboot.demo.config;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import com.vpgame.core.config.MysqlDbConfig;
+
+/**
+ *
+ * @author LZ
+ *@data 2017年7月5号
+ */
+
+public class MysqlDbUtil {
+ /**
+ * 生成mysql dburl
+ *
+ * @param dbConfig zk中的mysql配置项
+ * @param extraParams 额外的数据库配置参数
+ * @return mysql连接url
+ */
+ public static String generateDbUrl(MysqlDbConfig mysqlConfig,Map extraParams){
+ //下面定义一个变量用来存放url
+ StringBuilder url = new StringBuilder("jdbc:mysql://");
+ url.append(mysqlConfig.getHost());
+ url.append(":");
+ url.append(mysqlConfig.getPort());
+ url.append("/");
+ url.append(mysqlConfig.getDbname());
+ url.append("?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
+
+ if(null != extraParams){
+ for(Entry entry : extraParams.entrySet()) {
+ url.append("&").append(entry.getKey()).append("=").append(entry.getValue());
+ }
+ }
+ return url.toString();
+ }
+
+ public static String generateDbUrl(MysqlDbConfig mysqlDbConfig){
+ return generateDbUrl(mysqlDbConfig,null);
+ }
+}
diff --git a/src/main/java/com/springboot/demo/config/WebSecurityConfig.java b/src/main/java/com/springboot/demo/config/WebSecurityConfig.java
new file mode 100644
index 0000000..140a1ec
--- /dev/null
+++ b/src/main/java/com/springboot/demo/config/WebSecurityConfig.java
@@ -0,0 +1,72 @@
+package com.springboot.demo.config;
+
+import java.io.IOException;
+import java.util.List;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+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;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.authentication.AuthenticationFailureHandler;
+import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
+
+import com.springboot.demo.pojo.primary.FirstUser;
+import com.springboot.demo.service.impl.UserService;
+
+@Configuration
+@EnableWebSecurity
+public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Autowired
+ UserService userService;
+ // 这里设置允许那些路径下的页面需要设置安全验证
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ //如果直接输入登陆网址时,当登陆成功后,默认成功的跳转地址为“/”,现在发现的问题是这个不能将登陆成功后的数据渲染给模板
+ http
+ .authorizeRequests().antMatchers("/","/registry").permitAll().anyRequest().authenticated().and()
+ .formLogin().loginPage("/login").successHandler(new AuthenticationSuccessHandler(){
+ @Override
+ public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res,
+ Authentication auth) throws IOException, ServletException {
+ HttpSession session = req.getSession();
+ List userList = userService.findUserByName(auth.getName());
+ for(FirstUser user :userList){
+ //这里将userList中的数据加入到session中,这里加入的只有列表最后一个user
+ session.setAttribute("user", user);
+ }
+ res.sendRedirect("/success");
+ }
+ })
+ .failureHandler(new AuthenticationFailureHandler(){
+
+ @Override
+ public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
+ AuthenticationException exception) throws IOException, ServletException {
+ // 这里目前登陆失败的的验证我还没有写
+
+ }
+ }).permitAll().and()
+ .logout().permitAll();
+ }
+
+ @Autowired
+ protected void configGloable(AuthenticationManagerBuilder auth)throws Exception{
+
+ //这里下面我不知道设置的这个用户名和密码好像是固定好的了,理论上应该使用从数据库中查询出来的用户名和密码
+ //之前设置的一直有问题,加了默认成功页面时,可以跳转!但是每次从系启动应用时,第一次都会出现没有访问权限问题
+ //这里设置的前提是讲login页面中的username属性的标签的name改为username,这里我猜是这里的user验证时,取出的是前面名字为username的标签
+ auth.inMemoryAuthentication().withUser("李阵").password("lizhen").roles("USER");
+
+ //auth.userDetailsService(this.loginService);
+ }
+}
diff --git a/src/main/java/com/springboot/demo/controller/LoginController.java b/src/main/java/com/springboot/demo/controller/LoginController.java
index b654398..e649486 100644
--- a/src/main/java/com/springboot/demo/controller/LoginController.java
+++ b/src/main/java/com/springboot/demo/controller/LoginController.java
@@ -7,8 +7,6 @@
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseBody;
-
import com.springboot.demo.pojo.primary.FirstUser;
import com.springboot.demo.service.IUserService;
import com.springboot.demo.service.impl.LoginService;
@@ -23,12 +21,19 @@ public class LoginController {
@Autowired
IUserService userService;
-
- @RequestMapping("/login")
+ //现在这个数据是经过security模块传过来的
+ @RequestMapping("/login" )
public String login(){
return"login";
}
+ @RequestMapping("/success")
+ public String success(HttpSession session,Model model){
+ FirstUser user = (FirstUser) session.getAttribute("user");
+ model.addAttribute("user", user);
+ return "success";
+ }
+
@RequestMapping("/registry")
public String registry(){
return "user/registry";
@@ -36,8 +41,8 @@ public String registry(){
@RequestMapping(value="/registry", method={RequestMethod.POST})
- public String registry(Model model,String name, String password, String address, int age){
- FirstUser user = new FirstUser(null,name,password,address,age);
+ public String registry(Model model,String username, String password, String address, int age){
+ FirstUser user = new FirstUser(null,username,password,address,age);
userService.saveUser(user);
//用户注册后,跳转到登陆成功的界面,这里用户插入后,没有在查询用户操作
model.addAttribute("user", user);
@@ -45,6 +50,7 @@ public String registry(Model model,String name, String password, String address
}
+ //使用了权限认证后,好像所有的登陆都不会转发到这里来了
@RequestMapping(value = "/login", method = { RequestMethod.POST })
public String login(Model model, HttpSession session, String name, String password) {
// 这里调用业务层查看是否有用户
diff --git a/src/main/java/com/springboot/demo/controller/UserController.java b/src/main/java/com/springboot/demo/controller/UserController.java
index 715e60c..63a2178 100644
--- a/src/main/java/com/springboot/demo/controller/UserController.java
+++ b/src/main/java/com/springboot/demo/controller/UserController.java
@@ -48,9 +48,9 @@ public String update(Model model,Long id){
return "user/update";
}
@RequestMapping(value="/update",method={RequestMethod.POST})
- public String update(Model model,Long id,String name, String address, int age){
+ public String update(Model model,Long id,String username, String address, int age){
FirstUser user = userService.findUserById(id);
- user.setName(name);
+ user.setName(username);
user.setAddress(address);
user.setAge(age);
//执行跟新操作
diff --git a/src/main/java/com/springboot/demo/pojo/primary/FirstUser.java b/src/main/java/com/springboot/demo/pojo/primary/FirstUser.java
index 0837b7c..49a4f62 100644
--- a/src/main/java/com/springboot/demo/pojo/primary/FirstUser.java
+++ b/src/main/java/com/springboot/demo/pojo/primary/FirstUser.java
@@ -1,8 +1,5 @@
package com.springboot.demo.pojo.primary;
-import java.util.Arrays;
-import java.util.Collection;
-
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 1696948..359c73d 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,13 +1,18 @@
server.port=8080
#使用多数据库
-#第一个数据哭链接的参数
-spring.datasource.primary.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
-spring.datasource.primary.username=lizhen
-spring.datasource.primary.password=lizhen
+#第一个数据库链接的参数
+#spring.datasource.primary.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
+#spring.datasource.primary.username=lizhen
+#spring.datasource.primary.password=lizhen
+
+
+#这里使用zookeeper中配置信息代替上面的信息,这里的前缀在相应的Customproperty的配置类上制定
+com.springboot.demo.zookeeper-url = @zookeeper-url@
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
+
#第二个数据库连接的参数
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/springboot1?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.secondary.username=lizhen
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html
index f0256a6..5e8ed49 100644
--- a/src/main/resources/templates/index.html
+++ b/src/main/resources/templates/index.html
@@ -2,9 +2,19 @@
欢迎界面
+
-欢迎来到用户管理
-点击这里进入我的信息
+欢迎来到用户管理
+点击这里进入个人信息
\ No newline at end of file
diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html
index 89525d3..d392a6a 100644
--- a/src/main/resources/templates/login.html
+++ b/src/main/resources/templates/login.html
@@ -22,7 +22,7 @@