From de8e29f4f9d000c88219a092633f3a85fabd6928 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 3 Feb 2020 11:21:55 +0800 Subject: [PATCH] init code --- .gitignore | 29 + pom.xml | 258 +++++++ .../fit2cloud/metersphere/Application.java | 14 + .../metersphere/base/domain/User.java | 77 ++ .../metersphere/base/domain/UserExample.java | 670 ++++++++++++++++++ .../metersphere/base/mapper/UserMapper.java | 31 + .../metersphere/base/mapper/UserMapper.xml | 243 +++++++ .../metersphere/config/MybatisConfig.java | 33 + .../metersphere/config/ShiroConfig.java | 133 ++++ .../metersphere/config/WebConfig.java | 15 + .../metersphere/config/WebSocketConfig.java | 15 + .../controller/IndexController.java | 29 + .../controller/LoginController.java | 64 ++ .../metersphere/controller/ResultHolder.java | 73 ++ .../controller/TestController.java | 31 + .../controller/request/LoginRequest.java | 24 + .../interceptor/TestInterceptor.java | 17 + .../metersphere/security/LoginFilter.java | 36 + .../metersphere/security/ShiroDBRealm.java | 55 ++ .../metersphere/user/SessionUser.java | 26 + .../metersphere/user/SessionUtils.java | 18 + .../websocket/ServerEndpointConfigurator.java | 17 + .../websocket/WebSocketServer.java | 38 + src/main/resources/application.properties | 26 + src/main/resources/generatorConfig.xml | 50 ++ src/main/resources/logback.xml | 162 +++++ .../metersphere/ApplicationTests.java | 25 + 27 files changed, 2209 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/fit2cloud/metersphere/Application.java create mode 100644 src/main/java/com/fit2cloud/metersphere/base/domain/User.java create mode 100644 src/main/java/com/fit2cloud/metersphere/base/domain/UserExample.java create mode 100644 src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.java create mode 100644 src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.xml create mode 100644 src/main/java/com/fit2cloud/metersphere/config/MybatisConfig.java create mode 100644 src/main/java/com/fit2cloud/metersphere/config/ShiroConfig.java create mode 100644 src/main/java/com/fit2cloud/metersphere/config/WebConfig.java create mode 100644 src/main/java/com/fit2cloud/metersphere/config/WebSocketConfig.java create mode 100644 src/main/java/com/fit2cloud/metersphere/controller/IndexController.java create mode 100644 src/main/java/com/fit2cloud/metersphere/controller/LoginController.java create mode 100644 src/main/java/com/fit2cloud/metersphere/controller/ResultHolder.java create mode 100644 src/main/java/com/fit2cloud/metersphere/controller/TestController.java create mode 100644 src/main/java/com/fit2cloud/metersphere/controller/request/LoginRequest.java create mode 100644 src/main/java/com/fit2cloud/metersphere/interceptor/TestInterceptor.java create mode 100644 src/main/java/com/fit2cloud/metersphere/security/LoginFilter.java create mode 100644 src/main/java/com/fit2cloud/metersphere/security/ShiroDBRealm.java create mode 100644 src/main/java/com/fit2cloud/metersphere/user/SessionUser.java create mode 100644 src/main/java/com/fit2cloud/metersphere/user/SessionUtils.java create mode 100644 src/main/java/com/fit2cloud/metersphere/websocket/ServerEndpointConfigurator.java create mode 100644 src/main/java/com/fit2cloud/metersphere/websocket/WebSocketServer.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/generatorConfig.xml create mode 100644 src/main/resources/logback.xml create mode 100644 src/test/java/com/fit2cloud/metersphere/ApplicationTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..66cbb337c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# Created by .ignore support plugin (hsz.mobi) +.DS_Store +node_modules +node/ +/dist + +# local env files +.env.local +.env.*.local + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Editor directories and files +.idea +frontend.iml +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? + + +src/main/resources/static +src/main/resources/templates +target \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000..3a23a9eefe --- /dev/null +++ b/pom.xml @@ -0,0 +1,258 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.2.RELEASE + + + + 4.0.0 + + metersphere-server + + + UTF-8 + 1.4.0 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + spring-boot-starter-tomcat + org.springframework.boot + + + hibernate-validator + org.hibernate.validator + + + + + + org.springframework.boot + spring-boot-starter + + + + + + org.springframework.boot + spring-boot-starter-test + + + org.springframework.boot + spring-boot-starter-aop + + + org.springframework.boot + spring-boot-starter-jetty + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + + org.springframework.boot + spring-boot-starter-websocket + + + + mysql + mysql-connector-java + runtime + + + com.github.pagehelper + pagehelper + 5.0.3 + + + + org.apache.shiro + shiro-core + ${shiro.version} + + + commons-collections + commons-collections + + + commons-beanutils + commons-beanutils + + + + + org.apache.shiro + shiro-web + ${shiro.version} + + + org.apache.shiro + shiro-spring + ${shiro.version} + + + + org.apache.commons + commons-lang3 + + + junit + junit + + + + com.alibaba + fastjson + 1.2.45 + + + + org.slf4j + slf4j-simple + + + + + + + + src/main/java + + **/*.properties + **/*.xml + + false + + + src/main/resources + + **/*.* + + false + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12.4 + + true + + + + maven-source-plugin + 3.0.0 + + true + + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.6 + + + + org.apache.maven.plugins + maven-antrun-plugin + + + main-class-placement + generate-resources + + + + + + + + + + + + + + + + run + + + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.7 + + true + true + + + + mysql + mysql-connector-java + 5.1.41 + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/Application.java b/src/main/java/com/fit2cloud/metersphere/Application.java new file mode 100644 index 0000000000..d0878d722c --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/Application.java @@ -0,0 +1,14 @@ +package com.fit2cloud.metersphere; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration; +import org.springframework.boot.web.servlet.ServletComponentScan; + +@SpringBootApplication(exclude = {QuartzAutoConfiguration.class}) +@ServletComponentScan +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/base/domain/User.java b/src/main/java/com/fit2cloud/metersphere/base/domain/User.java new file mode 100644 index 0000000000..3e0860a7fa --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/base/domain/User.java @@ -0,0 +1,77 @@ +package com.fit2cloud.metersphere.base.domain; + +import java.io.Serializable; + +public class User implements Serializable { + private String id; + + private String name; + + private String email; + + private String phone; + + private String status; + + private Long createTime; + + private Long updateTime; + + private static final long serialVersionUID = 1L; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id == null ? null : id.trim(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name == null ? null : name.trim(); + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email == null ? null : email.trim(); + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone == null ? null : phone.trim(); + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status == null ? null : status.trim(); + } + + public Long getCreateTime() { + return createTime; + } + + public void setCreateTime(Long createTime) { + this.createTime = createTime; + } + + public Long getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(Long updateTime) { + this.updateTime = updateTime; + } +} \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/base/domain/UserExample.java b/src/main/java/com/fit2cloud/metersphere/base/domain/UserExample.java new file mode 100644 index 0000000000..dfb26ae9f6 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/base/domain/UserExample.java @@ -0,0 +1,670 @@ +package com.fit2cloud.metersphere.base.domain; + +import java.util.ArrayList; +import java.util.List; + +public class UserExample { + protected String orderByClause; + + protected boolean distinct; + + protected List oredCriteria; + + public UserExample() { + oredCriteria = new ArrayList(); + } + + public void setOrderByClause(String orderByClause) { + this.orderByClause = orderByClause; + } + + public String getOrderByClause() { + return orderByClause; + } + + public void setDistinct(boolean distinct) { + this.distinct = distinct; + } + + public boolean isDistinct() { + return distinct; + } + + public List getOredCriteria() { + return oredCriteria; + } + + public void or(Criteria criteria) { + oredCriteria.add(criteria); + } + + public Criteria or() { + Criteria criteria = createCriteriaInternal(); + oredCriteria.add(criteria); + return criteria; + } + + public Criteria createCriteria() { + Criteria criteria = createCriteriaInternal(); + if (oredCriteria.size() == 0) { + oredCriteria.add(criteria); + } + return criteria; + } + + protected Criteria createCriteriaInternal() { + Criteria criteria = new Criteria(); + return criteria; + } + + public void clear() { + oredCriteria.clear(); + orderByClause = null; + distinct = false; + } + + protected abstract static class GeneratedCriteria { + protected List criteria; + + protected GeneratedCriteria() { + super(); + criteria = new ArrayList(); + } + + public boolean isValid() { + return criteria.size() > 0; + } + + public List getAllCriteria() { + return criteria; + } + + public List getCriteria() { + return criteria; + } + + protected void addCriterion(String condition) { + if (condition == null) { + throw new RuntimeException("Value for condition cannot be null"); + } + criteria.add(new Criterion(condition)); + } + + protected void addCriterion(String condition, Object value, String property) { + if (value == null) { + throw new RuntimeException("Value for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value)); + } + + protected void addCriterion(String condition, Object value1, Object value2, String property) { + if (value1 == null || value2 == null) { + throw new RuntimeException("Between values for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value1, value2)); + } + + public Criteria andIdIsNull() { + addCriterion("id is null"); + return (Criteria) this; + } + + public Criteria andIdIsNotNull() { + addCriterion("id is not null"); + return (Criteria) this; + } + + public Criteria andIdEqualTo(String value) { + addCriterion("id =", value, "id"); + return (Criteria) this; + } + + public Criteria andIdNotEqualTo(String value) { + addCriterion("id <>", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThan(String value) { + addCriterion("id >", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThanOrEqualTo(String value) { + addCriterion("id >=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThan(String value) { + addCriterion("id <", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThanOrEqualTo(String value) { + addCriterion("id <=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLike(String value) { + addCriterion("id like", value, "id"); + return (Criteria) this; + } + + public Criteria andIdNotLike(String value) { + addCriterion("id not like", value, "id"); + return (Criteria) this; + } + + public Criteria andIdIn(List values) { + addCriterion("id in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdNotIn(List values) { + addCriterion("id not in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdBetween(String value1, String value2) { + addCriterion("id between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andIdNotBetween(String value1, String value2) { + addCriterion("id not between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andNameIsNull() { + addCriterion("name is null"); + return (Criteria) this; + } + + public Criteria andNameIsNotNull() { + addCriterion("name is not null"); + return (Criteria) this; + } + + public Criteria andNameEqualTo(String value) { + addCriterion("name =", value, "name"); + return (Criteria) this; + } + + public Criteria andNameNotEqualTo(String value) { + addCriterion("name <>", value, "name"); + return (Criteria) this; + } + + public Criteria andNameGreaterThan(String value) { + addCriterion("name >", value, "name"); + return (Criteria) this; + } + + public Criteria andNameGreaterThanOrEqualTo(String value) { + addCriterion("name >=", value, "name"); + return (Criteria) this; + } + + public Criteria andNameLessThan(String value) { + addCriterion("name <", value, "name"); + return (Criteria) this; + } + + public Criteria andNameLessThanOrEqualTo(String value) { + addCriterion("name <=", value, "name"); + return (Criteria) this; + } + + public Criteria andNameLike(String value) { + addCriterion("name like", value, "name"); + return (Criteria) this; + } + + public Criteria andNameNotLike(String value) { + addCriterion("name not like", value, "name"); + return (Criteria) this; + } + + public Criteria andNameIn(List values) { + addCriterion("name in", values, "name"); + return (Criteria) this; + } + + public Criteria andNameNotIn(List values) { + addCriterion("name not in", values, "name"); + return (Criteria) this; + } + + public Criteria andNameBetween(String value1, String value2) { + addCriterion("name between", value1, value2, "name"); + return (Criteria) this; + } + + public Criteria andNameNotBetween(String value1, String value2) { + addCriterion("name not between", value1, value2, "name"); + return (Criteria) this; + } + + public Criteria andEmailIsNull() { + addCriterion("email is null"); + return (Criteria) this; + } + + public Criteria andEmailIsNotNull() { + addCriterion("email is not null"); + return (Criteria) this; + } + + public Criteria andEmailEqualTo(String value) { + addCriterion("email =", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotEqualTo(String value) { + addCriterion("email <>", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailGreaterThan(String value) { + addCriterion("email >", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailGreaterThanOrEqualTo(String value) { + addCriterion("email >=", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailLessThan(String value) { + addCriterion("email <", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailLessThanOrEqualTo(String value) { + addCriterion("email <=", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailLike(String value) { + addCriterion("email like", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotLike(String value) { + addCriterion("email not like", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailIn(List values) { + addCriterion("email in", values, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotIn(List values) { + addCriterion("email not in", values, "email"); + return (Criteria) this; + } + + public Criteria andEmailBetween(String value1, String value2) { + addCriterion("email between", value1, value2, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotBetween(String value1, String value2) { + addCriterion("email not between", value1, value2, "email"); + return (Criteria) this; + } + + public Criteria andPhoneIsNull() { + addCriterion("phone is null"); + return (Criteria) this; + } + + public Criteria andPhoneIsNotNull() { + addCriterion("phone is not null"); + return (Criteria) this; + } + + public Criteria andPhoneEqualTo(String value) { + addCriterion("phone =", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneNotEqualTo(String value) { + addCriterion("phone <>", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneGreaterThan(String value) { + addCriterion("phone >", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneGreaterThanOrEqualTo(String value) { + addCriterion("phone >=", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneLessThan(String value) { + addCriterion("phone <", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneLessThanOrEqualTo(String value) { + addCriterion("phone <=", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneLike(String value) { + addCriterion("phone like", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneNotLike(String value) { + addCriterion("phone not like", value, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneIn(List values) { + addCriterion("phone in", values, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneNotIn(List values) { + addCriterion("phone not in", values, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneBetween(String value1, String value2) { + addCriterion("phone between", value1, value2, "phone"); + return (Criteria) this; + } + + public Criteria andPhoneNotBetween(String value1, String value2) { + addCriterion("phone not between", value1, value2, "phone"); + return (Criteria) this; + } + + public Criteria andStatusIsNull() { + addCriterion("status is null"); + return (Criteria) this; + } + + public Criteria andStatusIsNotNull() { + addCriterion("status is not null"); + return (Criteria) this; + } + + public Criteria andStatusEqualTo(String value) { + addCriterion("status =", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotEqualTo(String value) { + addCriterion("status <>", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusGreaterThan(String value) { + addCriterion("status >", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusGreaterThanOrEqualTo(String value) { + addCriterion("status >=", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLessThan(String value) { + addCriterion("status <", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLessThanOrEqualTo(String value) { + addCriterion("status <=", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLike(String value) { + addCriterion("status like", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotLike(String value) { + addCriterion("status not like", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusIn(List values) { + addCriterion("status in", values, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotIn(List values) { + addCriterion("status not in", values, "status"); + return (Criteria) this; + } + + public Criteria andStatusBetween(String value1, String value2) { + addCriterion("status between", value1, value2, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotBetween(String value1, String value2) { + addCriterion("status not between", value1, value2, "status"); + return (Criteria) this; + } + + public Criteria andCreateTimeIsNull() { + addCriterion("create_time is null"); + return (Criteria) this; + } + + public Criteria andCreateTimeIsNotNull() { + addCriterion("create_time is not null"); + return (Criteria) this; + } + + public Criteria andCreateTimeEqualTo(Long value) { + addCriterion("create_time =", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeNotEqualTo(Long value) { + addCriterion("create_time <>", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeGreaterThan(Long value) { + addCriterion("create_time >", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeGreaterThanOrEqualTo(Long value) { + addCriterion("create_time >=", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeLessThan(Long value) { + addCriterion("create_time <", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeLessThanOrEqualTo(Long value) { + addCriterion("create_time <=", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeIn(List values) { + addCriterion("create_time in", values, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeNotIn(List values) { + addCriterion("create_time not in", values, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeBetween(Long value1, Long value2) { + addCriterion("create_time between", value1, value2, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeNotBetween(Long value1, Long value2) { + addCriterion("create_time not between", value1, value2, "createTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeIsNull() { + addCriterion("update_time is null"); + return (Criteria) this; + } + + public Criteria andUpdateTimeIsNotNull() { + addCriterion("update_time is not null"); + return (Criteria) this; + } + + public Criteria andUpdateTimeEqualTo(Long value) { + addCriterion("update_time =", value, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeNotEqualTo(Long value) { + addCriterion("update_time <>", value, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeGreaterThan(Long value) { + addCriterion("update_time >", value, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeGreaterThanOrEqualTo(Long value) { + addCriterion("update_time >=", value, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeLessThan(Long value) { + addCriterion("update_time <", value, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeLessThanOrEqualTo(Long value) { + addCriterion("update_time <=", value, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeIn(List values) { + addCriterion("update_time in", values, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeNotIn(List values) { + addCriterion("update_time not in", values, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeBetween(Long value1, Long value2) { + addCriterion("update_time between", value1, value2, "updateTime"); + return (Criteria) this; + } + + public Criteria andUpdateTimeNotBetween(Long value1, Long value2) { + addCriterion("update_time not between", value1, value2, "updateTime"); + return (Criteria) this; + } + } + + public static class Criteria extends GeneratedCriteria { + + protected Criteria() { + super(); + } + } + + public static class Criterion { + private String condition; + + private Object value; + + private Object secondValue; + + private boolean noValue; + + private boolean singleValue; + + private boolean betweenValue; + + private boolean listValue; + + private String typeHandler; + + public String getCondition() { + return condition; + } + + public Object getValue() { + return value; + } + + public Object getSecondValue() { + return secondValue; + } + + public boolean isNoValue() { + return noValue; + } + + public boolean isSingleValue() { + return singleValue; + } + + public boolean isBetweenValue() { + return betweenValue; + } + + public boolean isListValue() { + return listValue; + } + + public String getTypeHandler() { + return typeHandler; + } + + protected Criterion(String condition) { + super(); + this.condition = condition; + this.typeHandler = null; + this.noValue = true; + } + + protected Criterion(String condition, Object value, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.typeHandler = typeHandler; + if (value instanceof List) { + this.listValue = true; + } else { + this.singleValue = true; + } + } + + protected Criterion(String condition, Object value) { + this(condition, value, null); + } + + protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.secondValue = secondValue; + this.typeHandler = typeHandler; + this.betweenValue = true; + } + + protected Criterion(String condition, Object value, Object secondValue) { + this(condition, value, secondValue, null); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.java b/src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.java new file mode 100644 index 0000000000..25809664bd --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.java @@ -0,0 +1,31 @@ +package com.fit2cloud.metersphere.base.mapper; + +import com.fit2cloud.metersphere.base.domain.User; +import com.fit2cloud.metersphere.base.domain.UserExample; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface UserMapper { + long countByExample(UserExample example); + + int deleteByExample(UserExample example); + + int deleteByPrimaryKey(String id); + + int insert(User record); + + int insertSelective(User record); + + List selectByExample(UserExample example); + + User selectByPrimaryKey(String id); + + int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); + + int updateByExample(@Param("record") User record, @Param("example") UserExample example); + + int updateByPrimaryKeySelective(User record); + + int updateByPrimaryKey(User record); +} \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.xml b/src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.xml new file mode 100644 index 0000000000..acaddee46d --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/base/mapper/UserMapper.xml @@ -0,0 +1,243 @@ + + + + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + id, name, email, phone, status, create_time, update_time + + + + + delete from user + where id = #{id,jdbcType=VARCHAR} + + + delete from user + + + + + + insert into user (id, name, email, + phone, status, create_time, + update_time) + values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, + #{phone,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT}, + #{updateTime,jdbcType=BIGINT}) + + + insert into user + + + id, + + + name, + + + email, + + + phone, + + + status, + + + create_time, + + + update_time, + + + + + #{id,jdbcType=VARCHAR}, + + + #{name,jdbcType=VARCHAR}, + + + #{email,jdbcType=VARCHAR}, + + + #{phone,jdbcType=VARCHAR}, + + + #{status,jdbcType=VARCHAR}, + + + #{createTime,jdbcType=BIGINT}, + + + #{updateTime,jdbcType=BIGINT}, + + + + + + update user + + + id = #{record.id,jdbcType=VARCHAR}, + + + name = #{record.name,jdbcType=VARCHAR}, + + + email = #{record.email,jdbcType=VARCHAR}, + + + phone = #{record.phone,jdbcType=VARCHAR}, + + + status = #{record.status,jdbcType=VARCHAR}, + + + create_time = #{record.createTime,jdbcType=BIGINT}, + + + update_time = #{record.updateTime,jdbcType=BIGINT}, + + + + + + + + update user + set id = #{record.id,jdbcType=VARCHAR}, + name = #{record.name,jdbcType=VARCHAR}, + email = #{record.email,jdbcType=VARCHAR}, + phone = #{record.phone,jdbcType=VARCHAR}, + status = #{record.status,jdbcType=VARCHAR}, + create_time = #{record.createTime,jdbcType=BIGINT}, + update_time = #{record.updateTime,jdbcType=BIGINT} + + + + + + update user + + + name = #{name,jdbcType=VARCHAR}, + + + email = #{email,jdbcType=VARCHAR}, + + + phone = #{phone,jdbcType=VARCHAR}, + + + status = #{status,jdbcType=VARCHAR}, + + + create_time = #{createTime,jdbcType=BIGINT}, + + + update_time = #{updateTime,jdbcType=BIGINT}, + + + where id = #{id,jdbcType=VARCHAR} + + + update user + set name = #{name,jdbcType=VARCHAR}, + email = #{email,jdbcType=VARCHAR}, + phone = #{phone,jdbcType=VARCHAR}, + status = #{status,jdbcType=VARCHAR}, + create_time = #{createTime,jdbcType=BIGINT}, + update_time = #{updateTime,jdbcType=BIGINT} + where id = #{id,jdbcType=VARCHAR} + + \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/config/MybatisConfig.java b/src/main/java/com/fit2cloud/metersphere/config/MybatisConfig.java new file mode 100644 index 0000000000..89a961a34d --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/config/MybatisConfig.java @@ -0,0 +1,33 @@ +package com.fit2cloud.metersphere.config; + +import com.github.pagehelper.PageInterceptor; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import java.util.Properties; + +@Configuration +@MapperScan(basePackages = "com.fit2cloud.metersphere.base.mapper", sqlSessionFactoryRef = "sqlSessionFactory") +@EnableTransactionManagement +@PropertySource(value = {"file:/opt/fit2cloud/conf/metersphere.properties"}, encoding = "UTF-8", ignoreResourceNotFound = true) +public class MybatisConfig { + + @Bean + @ConditionalOnMissingBean + public PageInterceptor pageInterceptor() { + PageInterceptor pageInterceptor = new PageInterceptor(); + Properties properties = new Properties(); + properties.setProperty("helperDialect", "mysql"); + properties.setProperty("rowBoundsWithCount", "true"); + properties.setProperty("reasonable", "true"); + properties.setProperty("offsetAsPageNum", "true"); + properties.setProperty("pageSizeZero", "true"); + pageInterceptor.setProperties(properties); + return pageInterceptor; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/config/ShiroConfig.java b/src/main/java/com/fit2cloud/metersphere/config/ShiroConfig.java new file mode 100644 index 0000000000..eab301c259 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/config/ShiroConfig.java @@ -0,0 +1,133 @@ +package com.fit2cloud.metersphere.config; + +import com.fit2cloud.metersphere.security.LoginFilter; +import com.fit2cloud.metersphere.security.ShiroDBRealm; +import org.apache.shiro.cache.MemoryConstrainedCacheManager; +import org.apache.shiro.session.mgt.SessionManager; +import org.apache.shiro.spring.LifecycleBeanPostProcessor; +import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; +import org.apache.shiro.web.servlet.SimpleCookie; +import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; +import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; + +import javax.servlet.DispatcherType; +import javax.servlet.Filter; +import java.util.EnumSet; +import java.util.Map; +import java.util.Objects; + +@Configuration +public class ShiroConfig { + + @Bean + public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager sessionManager) { + ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); + shiroFilterFactoryBean.getFilters().put("authc", new LoginFilter()); + shiroFilterFactoryBean.setLoginUrl("/login"); + shiroFilterFactoryBean.setSecurityManager(sessionManager); + shiroFilterFactoryBean.setUnauthorizedUrl("/403"); + shiroFilterFactoryBean.setSuccessUrl("/"); + + + Map filterChainDefinitionMap = shiroFilterFactoryBean.getFilterChainDefinitionMap(); + filterChainDefinitionMap.put("/resource/**", "anon"); + filterChainDefinitionMap.put("/", "anon"); + filterChainDefinitionMap.put("/signin", "anon"); + filterChainDefinitionMap.put("/isLogin", "anon"); + filterChainDefinitionMap.put("/css/**", "anon"); + filterChainDefinitionMap.put("/js/**", "anon"); + filterChainDefinitionMap.put("/img/**", "anon"); + filterChainDefinitionMap.put("/fonts/**", "anon"); + + filterChainDefinitionMap.put("/api/**", "anon"); + filterChainDefinitionMap.put("/403", "anon"); + filterChainDefinitionMap.put("/anonymous/**", "anon"); + filterChainDefinitionMap.put("/**", "authc"); + return shiroFilterFactoryBean; + } + + @Bean(name = "shiroFilter") + public FilterRegistrationBean shiroFilter(ShiroFilterFactoryBean shiroFilterFactoryBean) throws Exception { + FilterRegistrationBean registration = new FilterRegistrationBean<>(); + registration.setFilter((Filter) Objects.requireNonNull(shiroFilterFactoryBean.getObject())); + registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class)); + return registration; + } + + @Bean + public MemoryConstrainedCacheManager memoryConstrainedCacheManager() { + return new MemoryConstrainedCacheManager(); + } + + /** + * securityManager 不用直接注入shiroDBRealm,可能会导致事务失效 + * 解决方法见 handleContextRefresh + * http://www.debugrun.com/a/NKS9EJQ.html + */ + @Bean(name = "securityManager") + public DefaultWebSecurityManager securityManager(SessionManager sessionManager, MemoryConstrainedCacheManager memoryConstrainedCacheManager) { + DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager(); + dwsm.setSessionManager(sessionManager); + dwsm.setCacheManager(memoryConstrainedCacheManager); + return dwsm; + } + + @Bean(name = "shiroDBRealm") + @DependsOn("lifecycleBeanPostProcessor") + public ShiroDBRealm getShiroDBRealm() { + return new ShiroDBRealm(); + } + + @Bean(name = "lifecycleBeanPostProcessor") + public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { + return new LifecycleBeanPostProcessor(); + } + + @Bean + public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() { + DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator(); + daap.setProxyTargetClass(true); + return daap; + } + + @Bean + public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager sessionManager) { + AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor(); + aasa.setSecurityManager(sessionManager); + return new AuthorizationAttributeSourceAdvisor(); + } + + @Bean + public SessionManager sessionManager(MemoryConstrainedCacheManager memoryConstrainedCacheManager) { + DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); + sessionManager.setSessionIdUrlRewritingEnabled(false); + sessionManager.setGlobalSessionTimeout(1800000L); + sessionManager.setDeleteInvalidSessions(true); + sessionManager.setSessionValidationSchedulerEnabled(true); + SimpleCookie sessionIdCookie = new SimpleCookie(); + sessionManager.setSessionIdCookie(sessionIdCookie); + sessionIdCookie.setPath("/"); + sessionIdCookie.setName("MS_SESSION_ID"); + sessionManager.setCacheManager(memoryConstrainedCacheManager); + return sessionManager; + } + + /** + * 等到ApplicationContext 加载完成之后 装配shiroRealm + */ + @EventListener + public void handleContextRefresh(ContextRefreshedEvent event) { + ApplicationContext context = event.getApplicationContext(); + ShiroDBRealm shiroDBRealm = (ShiroDBRealm) context.getBean("shiroDBRealm"); + ((DefaultWebSecurityManager) context.getBean("securityManager")).setRealm(shiroDBRealm); + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/config/WebConfig.java b/src/main/java/com/fit2cloud/metersphere/config/WebConfig.java new file mode 100644 index 0000000000..09d42c5455 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/config/WebConfig.java @@ -0,0 +1,15 @@ +package com.fit2cloud.metersphere.config; + +import com.fit2cloud.metersphere.interceptor.TestInterceptor; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new TestInterceptor()); + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/config/WebSocketConfig.java b/src/main/java/com/fit2cloud/metersphere/config/WebSocketConfig.java new file mode 100644 index 0000000000..ca71eb80d7 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/config/WebSocketConfig.java @@ -0,0 +1,15 @@ +package com.fit2cloud.metersphere.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +@Configuration +public class WebSocketConfig { + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } + +} diff --git a/src/main/java/com/fit2cloud/metersphere/controller/IndexController.java b/src/main/java/com/fit2cloud/metersphere/controller/IndexController.java new file mode 100644 index 0000000000..ecf2aad575 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/controller/IndexController.java @@ -0,0 +1,29 @@ +package com.fit2cloud.metersphere.controller; + +import com.fit2cloud.metersphere.user.SessionUtils; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +@Controller +@RequestMapping +public class IndexController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView index() { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("index"); + return modelAndView; + } + + @GetMapping(value = "/login") + public String login() { + if (SessionUtils.getUser() == null) { + return "login"; + } else { + return "redirect:/"; + } + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/controller/LoginController.java b/src/main/java/com/fit2cloud/metersphere/controller/LoginController.java new file mode 100644 index 0000000000..1946b3e13c --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/controller/LoginController.java @@ -0,0 +1,64 @@ +package com.fit2cloud.metersphere.controller; + +import com.fit2cloud.metersphere.controller.request.LoginRequest; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.*; +import org.apache.shiro.authz.UnauthorizedException; +import org.apache.shiro.subject.Subject; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping +public class LoginController { + + @GetMapping(value = "/isLogin") + public ResultHolder isLogin() { + if (SecurityUtils.getSubject().isAuthenticated()) { + return ResultHolder.success(""); + } + return ResultHolder.error(""); + } + + @PostMapping(value = "/signin") + public ResultHolder login(@RequestBody LoginRequest request) { + String msg; + String username = StringUtils.trim(request.getUsername()); + String password = StringUtils.trim(request.getPassword()); + if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) { + return ResultHolder.error("user or password can't be null"); + } + + UsernamePasswordToken token = new UsernamePasswordToken(username, password); + Subject subject = SecurityUtils.getSubject(); + + try { + subject.login(token); + if (subject.isAuthenticated()) { + return ResultHolder.success(""); + } else { + return ResultHolder.error("login fail"); + } + } catch (ExcessiveAttemptsException e) { + msg = "excessive attempts"; + } catch (LockedAccountException e) { + msg = "the user has been locked."; + } catch (DisabledAccountException e) { + msg = "the user has been disabled. "; + } catch (ExpiredCredentialsException e) { + msg = "user expires. "; + } catch (AuthenticationException e) { + msg = e.getMessage(); + } catch (UnauthorizedException e) { + msg = "not authorized. " + e.getMessage(); + } + return ResultHolder.error(msg); + } + + @GetMapping(value = "/signout") + public ResultHolder logout() { + SecurityUtils.getSubject().logout(); + return ResultHolder.success(""); + } + +} diff --git a/src/main/java/com/fit2cloud/metersphere/controller/ResultHolder.java b/src/main/java/com/fit2cloud/metersphere/controller/ResultHolder.java new file mode 100644 index 0000000000..0937ea7e22 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/controller/ResultHolder.java @@ -0,0 +1,73 @@ +package com.fit2cloud.metersphere.controller; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +public class ResultHolder { + public ResultHolder() { + this.success = true; + } + + private ResultHolder(Object data) { + this.data = data; + this.success = true; + } + + private ResultHolder(boolean success, String msg) { + this.success = success; + this.message = msg; + } + + private ResultHolder(boolean success, String msg, Object data) { + this.success = success; + this.message = msg; + this.data = data; + } + + // 请求是否成功 + private boolean success = false; + // 描述信息 + private String message; + // 返回数据 + private Object data = ""; + + public boolean isSuccess() { + return this.success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public static ResultHolder success(Object obj) { + return new ResultHolder(obj); + } + + public static ResultHolder error(String message) { + return new ResultHolder(false, message, null); + } + + public static ResultHolder error(String message, Object object) { + return new ResultHolder(false, message, object); + } + + public String toString() { + return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/controller/TestController.java b/src/main/java/com/fit2cloud/metersphere/controller/TestController.java new file mode 100644 index 0000000000..e4043147f9 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/controller/TestController.java @@ -0,0 +1,31 @@ +package com.fit2cloud.metersphere.controller; + +import com.fit2cloud.metersphere.user.SessionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/test") +public class TestController { + + @GetMapping(value = "/{str}") + public Object getString(@PathVariable String str) throws InterruptedException { + if (StringUtils.equals("error", str)) { + throw new RuntimeException("test error"); + } + if (StringUtils.equals("warning", str)) { + return ResultHolder.error("test warning"); + } + if (StringUtils.equals("user", str)) { + return ResultHolder.success(SessionUtils.getUser()); + } + if (StringUtils.equals("sleep", str)) { + Thread.sleep(2000L); + return ResultHolder.success(str); + } + return ResultHolder.success(str); + } +} \ No newline at end of file diff --git a/src/main/java/com/fit2cloud/metersphere/controller/request/LoginRequest.java b/src/main/java/com/fit2cloud/metersphere/controller/request/LoginRequest.java new file mode 100644 index 0000000000..a9c39b66fb --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/controller/request/LoginRequest.java @@ -0,0 +1,24 @@ +package com.fit2cloud.metersphere.controller.request; + +public class LoginRequest { + + private String username; + + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/interceptor/TestInterceptor.java b/src/main/java/com/fit2cloud/metersphere/interceptor/TestInterceptor.java new file mode 100644 index 0000000000..412c880067 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/interceptor/TestInterceptor.java @@ -0,0 +1,17 @@ +package com.fit2cloud.metersphere.interceptor; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Component +public class TestInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { +// response.setHeader("Authentication-Status", "invalid"); + return true; + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/security/LoginFilter.java b/src/main/java/com/fit2cloud/metersphere/security/LoginFilter.java new file mode 100644 index 0000000000..8c7987d187 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/security/LoginFilter.java @@ -0,0 +1,36 @@ +package com.fit2cloud.metersphere.security; + +import com.alibaba.fastjson.JSONObject; +import com.fit2cloud.metersphere.controller.ResultHolder; +import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; + +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class LoginFilter extends FormAuthenticationFilter { + + @Override + protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { + if (((HttpServletRequest) request).getMethod().toUpperCase().equals("OPTIONS")) { + return true; + } + return super.isAccessAllowed(request, response, mappedValue); + } + + @Override + protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { + HttpServletRequest httpServletRequest = (HttpServletRequest) request; + if (httpServletRequest.getServletPath().endsWith("login")) { + return super.onAccessDenied(request, response); + } + HttpServletResponse httpServletResponse = (HttpServletResponse) response; + httpServletResponse.setCharacterEncoding("UTF-8"); + httpServletResponse.setContentType("application/json"); + httpServletResponse.setHeader("authentication-status", "invalid"); + ResultHolder result = ResultHolder.error("Authentication Status Invalid"); + httpServletResponse.getWriter().write(JSONObject.toJSON(result).toString()); + return true; + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/security/ShiroDBRealm.java b/src/main/java/com/fit2cloud/metersphere/security/ShiroDBRealm.java new file mode 100644 index 0000000000..fff730fbe8 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/security/ShiroDBRealm.java @@ -0,0 +1,55 @@ +package com.fit2cloud.metersphere.security; + + +import com.fit2cloud.metersphere.user.SessionUser; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.*; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; +import org.apache.shiro.subject.PrincipalCollection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * 自定义Realm 注入service 可能会导致在 service的aop 失效,例如@Transactional, + * 解决方法: + *

+ * 1. 这里改成注入mapper,这样mapper 中的事务失效
+ * 2. 这里仍然注入service,在配置ShiroConfig 的时候不去set realm, 等到spring 初始化完成之后 + * set realm + *

+ */ +public class ShiroDBRealm extends AuthorizingRealm { + + private Logger logger = LoggerFactory.getLogger(ShiroDBRealm.class); + + /** + * 权限认证 + */ + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { + return null; + } + + /** + * 登录认证 + */ + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { + UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; + String userId = token.getUsername(); + String password = String.valueOf(token.getPassword()); + SessionUser sessionUser = new SessionUser(); + + sessionUser.setName(userId); + sessionUser.setId(userId); + SecurityUtils.getSubject().getSession().setAttribute("user", sessionUser); + return new SimpleAuthenticationInfo(userId, password, getName()); + } + + @Override + public boolean isPermitted(PrincipalCollection principals, String permission) { + return true; + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/user/SessionUser.java b/src/main/java/com/fit2cloud/metersphere/user/SessionUser.java new file mode 100644 index 0000000000..3fe412ab9c --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/user/SessionUser.java @@ -0,0 +1,26 @@ +package com.fit2cloud.metersphere.user; + +import java.io.Serializable; + +public class SessionUser implements Serializable { + + private static final long serialVersionUID = -7149638440406959033L; + private String id; + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/user/SessionUtils.java b/src/main/java/com/fit2cloud/metersphere/user/SessionUtils.java new file mode 100644 index 0000000000..ceae1ae704 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/user/SessionUtils.java @@ -0,0 +1,18 @@ +package com.fit2cloud.metersphere.user; + +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.session.Session; +import org.apache.shiro.subject.Subject; + +public class SessionUtils { + + public static SessionUser getUser() { + try { + Subject subject = SecurityUtils.getSubject(); + Session session = subject.getSession(); + return (SessionUser) session.getAttribute("user"); + } catch (Exception e) { + return null; + } + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/websocket/ServerEndpointConfigurator.java b/src/main/java/com/fit2cloud/metersphere/websocket/ServerEndpointConfigurator.java new file mode 100644 index 0000000000..788cab51b3 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/websocket/ServerEndpointConfigurator.java @@ -0,0 +1,17 @@ +package com.fit2cloud.metersphere.websocket; + +import com.fit2cloud.metersphere.user.SessionUtils; + +import javax.websocket.HandshakeResponse; +import javax.websocket.server.HandshakeRequest; +import javax.websocket.server.ServerEndpointConfig; + +public class ServerEndpointConfigurator extends ServerEndpointConfig.Configurator { + @Override + public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { + // 将用户信息存储到socket的配置里 + System.out.println(SessionUtils.getUser()); + sec.getUserProperties().put("user", SessionUtils.getUser()); + super.modifyHandshake(sec, request, response); + } +} diff --git a/src/main/java/com/fit2cloud/metersphere/websocket/WebSocketServer.java b/src/main/java/com/fit2cloud/metersphere/websocket/WebSocketServer.java new file mode 100644 index 0000000000..60187ded24 --- /dev/null +++ b/src/main/java/com/fit2cloud/metersphere/websocket/WebSocketServer.java @@ -0,0 +1,38 @@ +package com.fit2cloud.metersphere.websocket; + +import com.fit2cloud.metersphere.user.SessionUtils; +import org.springframework.stereotype.Component; + +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; +import java.io.IOException; + +@ServerEndpoint(value = "/socket", configurator = ServerEndpointConfigurator.class) +@Component +public class WebSocketServer { + @OnOpen + public void onOpen(Session session) throws IOException { + // Get session and WebSocket connection + System.out.println("open: " + session.isOpen()); + System.out.println("open: " + SessionUtils.getUser()); + System.out.println("open: " + session.getUserProperties().get("user")); + } + + @OnMessage + public void onMessage(Session session, String message) throws IOException { + // Handle new messages + System.out.println(message); + } + + @OnClose + public void onClose(Session session) throws IOException { + // WebSocket connection closes + System.out.println("close: " + session.isOpen()); + } + + @OnError + public void onError(Session session, Throwable throwable) { + // Do error handling here + throwable.printStackTrace(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000000..28ebd79da5 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,26 @@ +spring.application.name=metersphere + +server.port=8888 + +# Hikari +spring.datasource.type=com.zaxxer.hikari.HikariDataSource +spring.datasource.hikari.minimum-idle=5 +spring.datasource.hikari.maximum-pool-size=15 +spring.datasource.hikari.auto-commit=true +spring.datasource.hikari.idle-timeout=30000 +spring.datasource.hikari.pool-name=DatebookHikariCP +spring.datasource.hikari.max-lifetime=1800000 +spring.datasource.hikari.connection-timeout=30000 +spring.datasource.hikari.connection-test-query=SELECT 1 + +# mybatis +mybatis.configuration.cache-enabled=true +mybatis.configuration.lazy-loading-enabled=false +mybatis.configuration.aggressive-lazy-loading=true +mybatis.configuration.multiple-result-sets-enabled=true +mybatis.configuration.use-column-label=true +mybatis.configuration.auto-mapping-behavior=full +mybatis.configuration.default-statement-timeout=25000 +mybatis.mapper-locations=classpath:mybatis/**/*.xml,classpath*:com/fit2cloud/metersphere/mapper/**/*.xml + +logging.file.path=/opt/fit2cloud/logs/${spring.application.name} \ No newline at end of file diff --git a/src/main/resources/generatorConfig.xml b/src/main/resources/generatorConfig.xml new file mode 100644 index 0000000000..db0befeeb1 --- /dev/null +++ b/src/main/resources/generatorConfig.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000000..b8e00b430e --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,162 @@ + + + + + + + + %d %5p %40.40c:%4L - %m%n + + + + + + DEBUG + ACCEPT + DENY + + ${logging.file.path}/debug.log + + ${logging.file.path}/history/debug.%d{yyyyMMdd}-%i.log + + 30 + + 50MB + + + + UTF-8 + %d [%thread] %-5level %logger{36} %line - %msg%n + + + + + + INFO + + ${logging.file.path}/info.log + + ${logging.file.path}/history/info.%d{yyyyMMdd}-%i.log + + 30 + + 50MB + + + + UTF-8 + %d [%thread] %-5level %logger{36} %line - %msg%n + + + + + + ERROR + ACCEPT + DENY + + + ${logging.file.path}/error.log + + ${logging.file.path}/history/error.%d{yyyyMMdd}-%i.log + + 30 + + 50MB + + + + UTF-8 + %d [%thread] %-5level %logger{36} %line - %msg%n + + + + + + WARN + ACCEPT + DENY + + ${logging.file.path}/warn.log + + ${logging.file.path}/history/warn.%d{yyyyMMdd}-%i.log + + 30 + + 50MB + + + + UTF-8 + %d [%thread] %-5level %logger{36} %line - %msg%n + + + + + + DEBUG + + 10000 + + + + + + DEBUG + + 10000 + + + + + + INFO + + 10000 + + + + + + ERROR + + 10000 + true + + + + + + WARN + + 10000 + true + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/fit2cloud/metersphere/ApplicationTests.java b/src/test/java/com/fit2cloud/metersphere/ApplicationTests.java new file mode 100644 index 0000000000..e3a46a17e7 --- /dev/null +++ b/src/test/java/com/fit2cloud/metersphere/ApplicationTests.java @@ -0,0 +1,25 @@ +package com.fit2cloud.metersphere; + + +import com.fit2cloud.metersphere.base.domain.User; +import com.fit2cloud.metersphere.base.mapper.UserMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ApplicationTests { + @Resource + UserMapper userMapper; + + @Test + public void test1() { + List users = userMapper.selectByExample(null); + System.out.println(users); + } +}