Skip to content

Commit 3b97213

Browse files
author
King Chang
committed
初始化檔案
1 parent fb716b0 commit 3b97213

49 files changed

Lines changed: 1899 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

javatutorial/pom.xml

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<project
2+
xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
5+
>
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>com.waterstart</groupId>
8+
<artifactId>javatutorial</artifactId>
9+
<packaging>war</packaging>
10+
<version>version</version>
11+
<name>Spring MVC Application</name>
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<java-version>1.8</java-version>
15+
<!-- Override Spring version -->
16+
<spring.version>4.2.1.RELEASE</spring.version>
17+
<!-- AssertJ is not a part of Spring IO platform, so the version must be provided explicitly -->
18+
<assertj-core-version>3.1.0</assertj-core-version>
19+
</properties>
20+
<parent>
21+
<groupId>io.spring.platform</groupId>
22+
<artifactId>platform-bom</artifactId>
23+
<version>1.1.3.RELEASE</version>
24+
<relativePath />
25+
</parent>
26+
<dependencies>
27+
<!-- Spring -->
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-webmvc</artifactId>
31+
</dependency>
32+
<!-- Security -->
33+
<dependency>
34+
<groupId>org.springframework.security</groupId>
35+
<artifactId>spring-security-config</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.security</groupId>
39+
<artifactId>spring-security-web</artifactId>
40+
</dependency>
41+
<!-- View -->
42+
<dependency>
43+
<groupId>org.thymeleaf</groupId>
44+
<artifactId>thymeleaf-spring4</artifactId>
45+
<!-- Avoid org.hibernate.jpa.boot.archive.spi.ArchiveException: Could not build ClassFile in Java 8 -->
46+
<exclusions>
47+
<exclusion>
48+
<artifactId>javassist</artifactId>
49+
<groupId>org.javassist</groupId>
50+
</exclusion>
51+
</exclusions>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.thymeleaf.extras</groupId>
55+
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
56+
</dependency>
57+
<!-- Persistence -->
58+
<dependency>
59+
<groupId>com.zaxxer</groupId>
60+
<artifactId>HikariCP</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-orm</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.hibernate</groupId>
68+
<artifactId>hibernate-entitymanager</artifactId>
69+
<!-- Avoid issue #72 Could not initialize class org.thymeleaf.templateresolver.ServletContextTemplateResolver due to 'validation is not supported' -->
70+
<exclusions>
71+
<exclusion>
72+
<artifactId>pull-parser</artifactId>
73+
<groupId>pull-parser</groupId>
74+
</exclusion>
75+
</exclusions>
76+
</dependency>
77+
<!-- Avoid: javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory' -->
78+
<dependency>
79+
<groupId>javax.el</groupId>
80+
<artifactId>javax.el-api</artifactId>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.hsqldb</groupId>
85+
<artifactId>hsqldb</artifactId>
86+
</dependency>
87+
<!-- Spring Data -->
88+
<dependency>
89+
<groupId>org.springframework.data</groupId>
90+
<artifactId>spring-data-jpa</artifactId>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.springframework.data</groupId>
94+
<artifactId>spring-data-mongodb</artifactId>
95+
</dependency>
96+
<!-- Validation -->
97+
<dependency>
98+
<groupId>org.hibernate</groupId>
99+
<artifactId>hibernate-validator</artifactId>
100+
</dependency>
101+
<!-- Logging -->
102+
<dependency>
103+
<groupId>org.slf4j</groupId>
104+
<artifactId>slf4j-api</artifactId>
105+
</dependency>
106+
<dependency>
107+
<groupId>ch.qos.logback</groupId>
108+
<artifactId>logback-classic</artifactId>
109+
</dependency>
110+
<!-- @Inject -->
111+
<dependency>
112+
<groupId>javax.inject</groupId>
113+
<artifactId>javax.inject</artifactId>
114+
</dependency>
115+
<!-- Servlet -->
116+
<dependency>
117+
<groupId>javax.servlet</groupId>
118+
<artifactId>javax.servlet-api</artifactId>
119+
<scope>provided</scope>
120+
</dependency>
121+
<!-- JSON -->
122+
<dependency>
123+
<groupId>com.fasterxml.jackson.core</groupId>
124+
<artifactId>jackson-databind</artifactId>
125+
</dependency>
126+
<!-- Utilities -->
127+
<dependency>
128+
<groupId>com.google.guava</groupId>
129+
<artifactId>guava</artifactId>
130+
</dependency>
131+
<!-- Test -->
132+
<dependency>
133+
<groupId>junit</groupId>
134+
<artifactId>junit</artifactId>
135+
<scope>test</scope>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.mockito</groupId>
139+
<artifactId>mockito-core</artifactId>
140+
<scope>test</scope>
141+
</dependency>
142+
<dependency>
143+
<groupId>org.assertj</groupId>
144+
<artifactId>assertj-core</artifactId>
145+
<version>${assertj-core-version}</version>
146+
<scope>test</scope>
147+
</dependency>
148+
<dependency>
149+
<groupId>org.hamcrest</groupId>
150+
<artifactId>hamcrest-core</artifactId>
151+
<scope>test</scope>
152+
</dependency>
153+
<dependency>
154+
<groupId>org.hamcrest</groupId>
155+
<artifactId>hamcrest-library</artifactId>
156+
<scope>test</scope>
157+
</dependency>
158+
<dependency>
159+
<groupId>org.objenesis</groupId>
160+
<artifactId>objenesis</artifactId>
161+
<scope>test</scope>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.springframework</groupId>
165+
<artifactId>spring-test</artifactId>
166+
<scope>test</scope>
167+
</dependency>
168+
</dependencies>
169+
<build>
170+
<plugins>
171+
<plugin>
172+
<groupId>org.apache.tomcat.maven</groupId>
173+
<artifactId>tomcat7-maven-plugin</artifactId>
174+
<version>2.2</version>
175+
<configuration>
176+
<path>/</path>
177+
</configuration>
178+
</plugin>
179+
<plugin>
180+
<groupId>org.apache.maven.plugins</groupId>
181+
<artifactId>maven-compiler-plugin</artifactId>
182+
<version>2.3.2</version>
183+
<configuration>
184+
<source>${java-version}</source>
185+
<target>${java-version}</target>
186+
</configuration>
187+
</plugin>
188+
<plugin>
189+
<groupId>org.apache.maven.plugins</groupId>
190+
<artifactId>maven-war-plugin</artifactId>
191+
<version>2.1.1</version>
192+
<configuration>
193+
<warName>javatutorial-version</warName>
194+
</configuration>
195+
</plugin>
196+
<plugin>
197+
<groupId>org.apache.maven.plugins</groupId>
198+
<artifactId>maven-dependency-plugin</artifactId>
199+
<executions>
200+
<execution>
201+
<id>deploy</id>
202+
<phase>deploy</phase>
203+
<goals>
204+
<goal>sources</goal>
205+
</goals>
206+
</execution>
207+
</executions>
208+
</plugin>
209+
<plugin>
210+
<groupId>org.apache.maven.plugins</groupId>
211+
<artifactId>maven-resources-plugin</artifactId>
212+
<version>2.7</version>
213+
<configuration>
214+
<encoding>${project.build.sourceEncoding}</encoding>
215+
</configuration>
216+
</plugin>
217+
</plugins>
218+
</build>
219+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.waterstart;
2+
3+
public interface Application {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.waterstart.account;
2+
3+
import javax.persistence.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonIgnore;
6+
7+
@SuppressWarnings("serial")
8+
@Entity
9+
@Table(name = "account")
10+
@NamedQuery(name = Account.FIND_BY_EMAIL, query = "select a from Account a where a.email = :email")
11+
public class Account implements java.io.Serializable {
12+
13+
public static final String FIND_BY_EMAIL = "Account.findByEmail";
14+
15+
@Id
16+
@GeneratedValue
17+
private Long id;
18+
19+
@Column(unique = true)
20+
private String email;
21+
22+
@JsonIgnore
23+
private String password;
24+
25+
private String role = "ROLE_USER";
26+
27+
protected Account() {
28+
29+
}
30+
31+
public Account(String email, String password, String role) {
32+
this.email = email;
33+
this.password = password;
34+
this.role = role;
35+
}
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public String getEmail() {
42+
return email;
43+
}
44+
45+
public void setEmail(String email) {
46+
this.email = email;
47+
}
48+
49+
public String getPassword() {
50+
return password;
51+
}
52+
53+
public void setPassword(String password) {
54+
this.password = password;
55+
}
56+
57+
public String getRole() {
58+
return role;
59+
}
60+
61+
public void setRole(String role) {
62+
this.role = role;
63+
}
64+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.waterstart.account;
2+
3+
import java.security.Principal;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.security.access.annotation.Secured;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.util.Assert;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
import org.springframework.web.bind.annotation.ResponseStatus;
14+
15+
@Controller
16+
@Secured("ROLE_USER")
17+
class AccountController {
18+
19+
private AccountRepository accountRepository;
20+
21+
@Autowired
22+
public AccountController(AccountRepository accountRepository) {
23+
this.accountRepository = accountRepository;
24+
}
25+
26+
@RequestMapping(value = "account/current", method = RequestMethod.GET)
27+
@ResponseStatus(value = HttpStatus.OK)
28+
@ResponseBody
29+
public Account accounts(Principal principal) {
30+
Assert.notNull(principal);
31+
return accountRepository.findByEmail(principal.getName());
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.waterstart.account;
2+
3+
import javax.persistence.*;
4+
import javax.inject.Inject;
5+
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.transaction.annotation.Transactional;
8+
import org.springframework.security.crypto.password.PasswordEncoder;
9+
10+
@Repository
11+
@Transactional(readOnly = true)
12+
public class AccountRepository {
13+
14+
@PersistenceContext
15+
private EntityManager entityManager;
16+
17+
@Inject
18+
private PasswordEncoder passwordEncoder;
19+
20+
@Transactional
21+
public Account save(Account account) {
22+
account.setPassword(passwordEncoder.encode(account.getPassword()));
23+
entityManager.persist(account);
24+
return account;
25+
}
26+
27+
public Account findByEmail(String email) {
28+
try {
29+
return entityManager.createNamedQuery(Account.FIND_BY_EMAIL, Account.class)
30+
.setParameter("email", email)
31+
.getSingleResult();
32+
} catch (PersistenceException e) {
33+
return null;
34+
}
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)