Selaa lähdekoodia

commit springMvc

zxy 2 vuotta sitten
vanhempi
commit
bd68cdefdd

+ 11 - 1
pom.xml

@@ -11,7 +11,7 @@
11
     <modules>
11
     <modules>
12
         <module>spring_annotation</module>
12
         <module>spring_annotation</module>
13
         <module>spring_mvc</module>
13
         <module>spring_mvc</module>
14
-        <module>springWeb</module>
14
+        <module>springMvc</module>
15
     </modules>
15
     </modules>
16
 
16
 
17
     <properties>
17
     <properties>
@@ -57,6 +57,16 @@
57
 
57
 
58
             <!--            <version></version>-->
58
             <!--            <version></version>-->
59
         </dependency>
59
         </dependency>
60
+        <dependency>
61
+            <groupId>com.auth0</groupId>
62
+            <artifactId>java-jwt</artifactId>
63
+            <version>3.10.3</version>
64
+        </dependency>
65
+        <dependency>
66
+            <groupId>com.alibaba</groupId>
67
+            <artifactId>fastjson</artifactId>
68
+            <version>1.2.72</version>
69
+        </dependency>
60
     </dependencies>
70
     </dependencies>
61
 
71
 
62
 </project>
72
 </project>

+ 7 - 2
springWeb/pom.xml

@@ -23,9 +23,9 @@
23
   <modelVersion>4.0.0</modelVersion>
23
   <modelVersion>4.0.0</modelVersion>
24
   <packaging>war</packaging>
24
   <packaging>war</packaging>
25
 
25
 
26
-  <name>springWeb</name>
26
+  <name>springMvc</name>
27
   <groupId>org.example</groupId>
27
   <groupId>org.example</groupId>
28
-  <artifactId>springWeb</artifactId>
28
+  <artifactId>springMvc</artifactId>
29
   <version>1.0-SNAPSHOT</version>
29
   <version>1.0-SNAPSHOT</version>
30
 
30
 
31
   <build>
31
   <build>
@@ -54,6 +54,11 @@
54
       <artifactId>[the artifact id of the block to be mounted]</artifactId>
54
       <artifactId>[the artifact id of the block to be mounted]</artifactId>
55
       <version>1.0-SNAPSHOT</version>
55
       <version>1.0-SNAPSHOT</version>
56
     </dependency-->
56
     </dependency-->
57
+    <dependency>
58
+      <groupId>org.springframework</groupId>
59
+      <artifactId>spring-webmvc</artifactId>
60
+      <version>5.3.6</version>
61
+    </dependency>
57
   </dependencies>
62
   </dependencies>
58
 
63
 
59
 </project>
64
 </project>

+ 43 - 0
springMvc/src/main/webapp/WEB-INF/applicationContext.xml

@@ -0,0 +1,43 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!--
3
+  Licensed to the Apache Software Foundation (ASF) under one or more
4
+  contributor license agreements.  See the NOTICE file distributed with
5
+  this work for additional information regarding copyright ownership.
6
+  The ASF licenses this file to You under the Apache License, Version 2.0
7
+  (the "License"); you may not use this file except in compliance with
8
+  the License.  You may obtain a copy of the License at
9
+
10
+      http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+  Unless required by applicable law or agreed to in writing, software
13
+  distributed under the License is distributed on an "AS IS" BASIS,
14
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+  See the License for the specific language governing permissions and
16
+  limitations under the License.
17
+-->
18
+<!-- @version $Id: applicationContext.xml 561608 2007-08-01 00:33:12Z vgritsenko $ -->
19
+<beans xmlns="http://www.springframework.org/schema/beans"
20
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21
+       xmlns:util="http://www.springframework.org/schema/util"
22
+       xmlns:configurator="http://cocoon.apache.org/schema/configurator"
23
+       xmlns:avalon="http://cocoon.apache.org/schema/avalon"
24
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
25
+                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
26
+                           http://cocoon.apache.org/schema/configurator http://cocoon.apache.org/schema/configurator/cocoon-configurator-1.0.1.xsd
27
+                           http://cocoon.apache.org/schema/avalon http://cocoon.apache.org/schema/avalon/cocoon-avalon-1.0.xsd">
28
+
29
+  <!-- Activate Cocoon Spring Configurator -->
30
+  <configurator:settings/>
31
+
32
+  <!-- Configure Log4j -->
33
+  <bean name="org.apache.cocoon.spring.configurator.log4j"
34
+        class="org.apache.cocoon.spring.configurator.log4j.Log4JConfigurator"
35
+        scope="singleton">
36
+    <property name="settings" ref="org.apache.cocoon.configuration.Settings"/>
37
+    <property name="resource" value="/WEB-INF/log4j.xml"/>
38
+  </bean>
39
+
40
+  <!-- Activate Avalon Bridge -->
41
+  <avalon:bridge/>
42
+
43
+</beans>

+ 38 - 0
springMvc/src/main/webapp/WEB-INF/log4j.xml

@@ -0,0 +1,38 @@
1
+<?xml version="1.0" encoding="UTF-8" ?>
2
+<!--
3
+  Licensed to the Apache Software Foundation (ASF) under one or more
4
+  contributor license agreements.  See the NOTICE file distributed with
5
+  this work for additional information regarding copyright ownership.
6
+  The ASF licenses this file to You under the Apache License, Version 2.0
7
+  (the "License"); you may not use this file except in compliance with
8
+  the License.  You may obtain a copy of the License at
9
+
10
+      http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+  Unless required by applicable law or agreed to in writing, software
13
+  distributed under the License is distributed on an "AS IS" BASIS,
14
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+  See the License for the specific language governing permissions and
16
+  limitations under the License.
17
+-->
18
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
19
+
20
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
21
+  <!--
22
+    - This is a sample configuration for log4j.
23
+    - It simply just logs everything into a single log file.
24
+    - Note, that you can use properties for value substitution.
25
+    -->
26
+  <appender name="CORE" class="org.apache.log4j.FileAppender">
27
+    <param name="File"   value="${org.apache.cocoon.work.directory}/cocoon-logs/log4j.log" />
28
+    <param name="Append" value="false" />
29
+    <layout class="org.apache.log4j.PatternLayout">
30
+      <param name="ConversionPattern" value="%d %-5p %t %c - %m%n"/>
31
+    </layout>
32
+  </appender>
33
+
34
+  <root>
35
+    <priority value="${org.apache.cocoon.log4j.loglevel}"/>
36
+    <appender-ref ref="CORE"/>
37
+  </root>
38
+</log4j:configuration>

+ 119 - 0
springMvc/src/main/webapp/WEB-INF/web.xml

@@ -0,0 +1,119 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!--
3
+  Licensed to the Apache Software Foundation (ASF) under one or more
4
+  contributor license agreements.  See the NOTICE file distributed with
5
+  this work for additional information regarding copyright ownership.
6
+  The ASF licenses this file to You under the Apache License, Version 2.0
7
+  (the "License"); you may not use this file except in compliance with
8
+  the License.  You may obtain a copy of the License at
9
+
10
+      http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+  Unless required by applicable law or agreed to in writing, software
13
+  distributed under the License is distributed on an "AS IS" BASIS,
14
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+  See the License for the specific language governing permissions and
16
+  limitations under the License.
17
+-->
18
+
19
+<!--
20
+  - This is the Cocoon web-app configurations file
21
+  -
22
+  - $Id$
23
+  -->
24
+<web-app version="2.4"
25
+         xmlns="http://java.sun.com/xml/ns/j2ee"
26
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27
+         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
28
+
29
+  <!-- Servlet Filters ================================================ -->
30
+
31
+  <!--
32
+    - Declare a filter for multipart MIME handling
33
+    -->
34
+  <filter>
35
+    <description>Multipart MIME handling filter for Cocoon</description>
36
+    <display-name>Cocoon multipart filter</display-name>
37
+    <filter-name>CocoonMultipartFilter</filter-name>
38
+    <filter-class>org.apache.cocoon.servlet.multipart.MultipartFilter</filter-class>
39
+  </filter>
40
+
41
+  <!--
42
+    - Declare a filter for debugging incoming request
43
+    -->
44
+  <filter>
45
+    <description>Log debug information about each request</description>
46
+    <display-name>Cocoon debug filter</display-name>
47
+    <filter-name>CocoonDebugFilter</filter-name>
48
+    <filter-class>org.apache.cocoon.servlet.DebugFilter</filter-class>
49
+  </filter>
50
+
51
+  <!-- Filter mappings ================================================ -->
52
+
53
+  <!--
54
+    - Use the Cocoon multipart filter together with the Cocoon demo webapp
55
+    -->
56
+  <filter-mapping>
57
+    <filter-name>CocoonMultipartFilter</filter-name>
58
+    <servlet-name>Cocoon</servlet-name>
59
+  </filter-mapping>
60
+  <filter-mapping>
61
+    <filter-name>CocoonMultipartFilter</filter-name>
62
+    <servlet-name>DispatcherServlet</servlet-name>
63
+  </filter-mapping>
64
+
65
+  <!--
66
+    - Use the Cocoon debug filter together with the Cocoon demo webapp
67
+  <filter-mapping>
68
+    <filter-name>CocoonDebugFilter</filter-name>
69
+    <servlet-name>Cocoon</servlet-name>
70
+  </filter-mapping>
71
+    -->
72
+
73
+  <!-- Servlet Context Listener ======================================= -->
74
+
75
+  <!--
76
+    - Declare Spring context listener which sets up the Spring Application Context
77
+    - containing all Cocoon components (and user defined beans as well).
78
+    -->
79
+  <listener>
80
+    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
81
+  </listener>
82
+
83
+  <!--
84
+    - Declare Spring request listener which sets up the required RequestAttributes
85
+    - to support Springs and Cocoon custom bean scopes like the request scope or the
86
+    - session scope.
87
+    -->
88
+  <listener>
89
+    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
90
+  </listener>
91
+
92
+  <!-- Servlet Configuration ========================================== -->
93
+
94
+  <!--
95
+    - Servlet that dispatches requests to the Spring managed block servlets
96
+    -->
97
+  <servlet>
98
+    <description>Cocoon blocks dispatcher</description>
99
+    <display-name>DispatcherServlet</display-name>
100
+    <servlet-name>DispatcherServlet</servlet-name>
101
+    <servlet-class>org.apache.cocoon.servletservice.DispatcherServlet</servlet-class>
102
+    <load-on-startup>1</load-on-startup>
103
+  </servlet>
104
+
105
+  <!-- URL space mappings ============================================= -->
106
+
107
+  <!--
108
+    - Cocoon handles all the URL space assigned to the webapp using its sitemap.
109
+    - It is recommended to leave it unchanged. Under some circumstances though
110
+    - (like integration with proprietary webapps or servlets) you might have
111
+    - to change this parameter.
112
+    -->
113
+  <servlet-mapping>
114
+    <servlet-name>DispatcherServlet</servlet-name>
115
+    <url-pattern>/*</url-pattern>
116
+  </servlet-mapping>
117
+
118
+</web-app>
119
+        

+ 9 - 1
spring_annotation/src/main/java/AnnotationMain.java

@@ -1,4 +1,7 @@
1
 import com.Config.SpringConfiguration;
1
 import com.Config.SpringConfiguration;
2
+import com.alibaba.druid.pool.DruidDataSource;
3
+import com.alibaba.druid.pool.DruidPooledConnection;
4
+import com.service.JwtService;
2
 import com.service.StudentService;
5
 import com.service.StudentService;
3
 import org.springframework.context.ApplicationContext;
6
 import org.springframework.context.ApplicationContext;
4
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -7,9 +10,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
7
  * 使用注解引入容器
10
  * 使用注解引入容器
8
  */
11
  */
9
 public class AnnotationMain {
12
 public class AnnotationMain {
10
-    public static void main(String[] args) {
13
+    public static void main(String[] args) throws Exception{
11
         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
14
         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
12
         Object jdbcUrl = applicationContext.getBean("getJdbcUrl");
15
         Object jdbcUrl = applicationContext.getBean("getJdbcUrl");
13
         StudentService bean = applicationContext.getBean(StudentService.class);
16
         StudentService bean = applicationContext.getBean(StudentService.class);
17
+        JwtService bean1 = applicationContext.getBean(JwtService.class);
18
+        String s = bean1.generateToken("123");
19
+        bean1.parseToken(s);
20
+        DruidDataSource dataSource = (DruidDataSource) applicationContext.getBean("dataSource");
21
+        DruidPooledConnection connection = dataSource.getConnection();
14
     }
22
     }
15
 }
23
 }

+ 19 - 0
spring_annotation/src/main/java/com/Config/SpringConfiguration.java

@@ -1,11 +1,14 @@
1
 package com.Config;
1
 package com.Config;
2
 
2
 
3
+import com.alibaba.druid.pool.DruidDataSource;
3
 import org.springframework.beans.factory.annotation.Value;
4
 import org.springframework.beans.factory.annotation.Value;
4
 import org.springframework.context.annotation.Bean;
5
 import org.springframework.context.annotation.Bean;
5
 import org.springframework.context.annotation.ComponentScan;
6
 import org.springframework.context.annotation.ComponentScan;
6
 import org.springframework.context.annotation.Configuration;
7
 import org.springframework.context.annotation.Configuration;
7
 import org.springframework.context.annotation.PropertySource;
8
 import org.springframework.context.annotation.PropertySource;
8
 
9
 
10
+import javax.sql.DataSource;
11
+
9
 /**
12
 /**
10
  * spring 核心配置文件类
13
  * spring 核心配置文件类
11
  */
14
  */
@@ -18,8 +21,24 @@ public class SpringConfiguration {
18
     // 写一个dataSource
21
     // 写一个dataSource
19
     @Value("${jdbc.url}")
22
     @Value("${jdbc.url}")
20
     String jdbcUrl;
23
     String jdbcUrl;
24
+    @Value("${jdbc.driver}")
25
+    String driver;
26
+    @Value("${jdbc.username}")
27
+    String userName;
28
+    @Value("${jdbc.password}")
29
+    String password;
21
     @Bean("getJdbcUrl")
30
     @Bean("getJdbcUrl")
22
     public String getJdbcUrl() {
31
     public String getJdbcUrl() {
23
         return jdbcUrl;
32
         return jdbcUrl;
24
     }
33
     }
34
+    @Bean("dataSource")
35
+    public DataSource dataSource() {
36
+        DruidDataSource druidDataSource = new DruidDataSource();
37
+        druidDataSource.setDriverClassName(driver);
38
+        druidDataSource.setUrl(jdbcUrl);
39
+        druidDataSource.setUsername(userName);
40
+        druidDataSource.setPassword(password);
41
+        return druidDataSource;
42
+    }
43
+
25
 }
44
 }

+ 6 - 0
spring_annotation/src/main/java/com/service/JwtService.java

@@ -0,0 +1,6 @@
1
+package com.service;
2
+
3
+public interface JwtService {
4
+     String generateToken(String userId);
5
+     void parseToken(String token);
6
+}

+ 38 - 0
spring_annotation/src/main/java/com/service/impl/JwtServiceImpl.java

@@ -0,0 +1,38 @@
1
+package com.service.impl;
2
+
3
+import com.alibaba.fastjson.JSON;
4
+import com.auth0.jwt.JWT;
5
+import com.auth0.jwt.JWTVerifier;
6
+import com.auth0.jwt.algorithms.Algorithm;
7
+import com.auth0.jwt.interfaces.Claim;
8
+import com.auth0.jwt.interfaces.DecodedJWT;
9
+import com.service.JwtService;
10
+import org.springframework.stereotype.Service;
11
+
12
+import java.util.Date;
13
+import java.util.HashMap;
14
+import java.util.Map;
15
+
16
+@Service
17
+public class JwtServiceImpl implements JwtService {
18
+    private static final String TOKEN_SECRET = "ZXY";
19
+    @Override
20
+    public String generateToken(String userId) {
21
+        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
22
+        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
23
+        stringObjectHashMap.put("type","jwt");
24
+        return JWT.create()
25
+                .withIssuedAt(new Date())
26
+                .withClaim("token","userToken")
27
+                .sign(algorithm);
28
+    }
29
+
30
+    @Override
31
+    public void parseToken(String token) {
32
+        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
33
+        JWTVerifier build = JWT.require(algorithm).build();
34
+        DecodedJWT verify = build.verify(token);
35
+        String pa = verify.getClaim("token").asString();
36
+//        Map hashMap = JSON.parseObject(pa, Map.class);
37
+    }
38
+}

+ 3 - 0
spring_annotation/src/main/java/com/service/impl/StudentServiceImpl.java

@@ -3,11 +3,14 @@ package com.service.impl;
3
 import com.dao.StudentDao;
3
 import com.dao.StudentDao;
4
 import com.service.StudentService;
4
 import com.service.StudentService;
5
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.beans.factory.annotation.Value;
6
 import org.springframework.stereotype.Service;
7
 import org.springframework.stereotype.Service;
7
 
8
 
8
 //<bean id="studentService" class="com.service.impl.StudentServiceImpl">
9
 //<bean id="studentService" class="com.service.impl.StudentServiceImpl">
9
 @Service
10
 @Service
10
 public class StudentServiceImpl implements StudentService {
11
 public class StudentServiceImpl implements StudentService {
12
+    @Value("${jdbc.url}")
13
+    private String jddc;
11
 //    <property name="studentDao" ref="studentDao"></property>
14
 //    <property name="studentDao" ref="studentDao"></property>
12
 //    @Autowired
15
 //    @Autowired
13
 //    @Qualifier("studentDao") // 按照id名称值从容器中进行匹配 配合autowired一起使用
16
 //    @Qualifier("studentDao") // 按照id名称值从容器中进行匹配 配合autowired一起使用