Skip to content

Commit 3440c39

Browse files
authored
Merge pull request #13 from ZakShearman/main
Move to REST server
2 parents af273c3 + 6b17ea0 commit 3440c39

61 files changed

Lines changed: 1132 additions & 1604 deletions

Some content is hidden

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

pom.xml

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.5.6</version>
8+
<version>2.6.3</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.imjustdoom</groupId>
@@ -30,13 +30,20 @@
3030
</dependency>
3131

3232
<dependency>
33-
<groupId>org.thymeleaf.extras</groupId>
34-
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-data-jpa</artifactId>
3535
</dependency>
3636

3737
<dependency>
38-
<groupId>org.springframework.boot</groupId>
39-
<artifactId>spring-boot-starter-data-jpa</artifactId>
38+
<groupId>com.querydsl</groupId>
39+
<artifactId>querydsl-apt</artifactId>
40+
<version>5.0.0</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.querydsl</groupId>
45+
<artifactId>querydsl-jpa</artifactId>
46+
<version>5.0.0</version>
4047
</dependency>
4148

4249
<dependency>
@@ -68,11 +75,6 @@
6875
<scope>test</scope>
6976
</dependency>
7077

71-
<dependency>
72-
<groupId>org.springframework.boot</groupId>
73-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
74-
</dependency>
75-
7678
<dependency>
7779
<groupId>org.springframework.boot</groupId>
7880
<artifactId>spring-boot-configuration-processor</artifactId>
@@ -104,25 +106,13 @@
104106
<scope>provided</scope>
105107
</dependency>
106108

107-
<dependency>
108-
<groupId>org.commonmark</groupId>
109-
<artifactId>commonmark</artifactId>
110-
<version>0.18.1</version>
111-
</dependency>
112-
113109
<dependency>
114110
<groupId>com.google.code.gson</groupId>
115111
<artifactId>gson</artifactId>
116112
<version>2.8.9</version>
117113
<scope>compile</scope>
118114
</dependency>
119115

120-
<dependency>
121-
<groupId>me.xdrop</groupId>
122-
<artifactId>fuzzywuzzy</artifactId>
123-
<version>1.3.1</version>
124-
</dependency>
125-
126116
</dependencies>
127117

128118
<repositories>
@@ -154,6 +144,22 @@
154144
<target>17</target>
155145
</configuration>
156146
</plugin>
147+
<plugin>
148+
<groupId>com.mysema.maven</groupId>
149+
<artifactId>apt-maven-plugin</artifactId>
150+
<version>1.1.3</version>
151+
<executions>
152+
<execution>
153+
<goals>
154+
<goal>process</goal>
155+
</goals>
156+
<configuration>
157+
<outputDirectory>target/generated-sources/java</outputDirectory>
158+
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
159+
</configuration>
160+
</execution>
161+
</executions>
162+
</plugin>
157163
<plugin>
158164
<groupId>org.springframework.boot</groupId>
159165
<artifactId>spring-boot-maven-plugin</artifactId>

src/main/java/com/imjustdoom/pluginsite/PluginSiteApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
6+
import org.springframework.data.web.config.EnableSpringDataWebSupport;
67

78
@SpringBootApplication
9+
@EnableSpringDataWebSupport
810
@ConfigurationPropertiesScan
911
public class PluginSiteApplication {
1012

src/main/java/com/imjustdoom/pluginsite/config/SecurityConfig.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.imjustdoom.pluginsite.config.exception;
2+
3+
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
5+
import org.springframework.http.HttpStatus;
6+
7+
public enum RestErrorCode {
8+
INVALID_USERNAME(HttpStatus.BAD_REQUEST, "auth", 1),
9+
INVALID_EMAIL(HttpStatus.BAD_REQUEST, "auth", 2),
10+
USERNAME_NOT_AVAILABLE(HttpStatus.BAD_REQUEST, "auth", 3),
11+
EMAIL_NOT_AVAILABLE(HttpStatus.BAD_REQUEST, "auth", 4),
12+
13+
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "auth", 100),
14+
FORBIDDEN(HttpStatus.FORBIDDEN, "auth", 101),
15+
16+
ACCOUNT_NOT_FOUND(HttpStatus.NOT_FOUND, "data", 100),
17+
REPORT_NOT_FOUND(HttpStatus.NOT_FOUND, "data", 101),
18+
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "data", 102),
19+
RESOURCE_UPDATE_NOT_FOUND(HttpStatus.NOT_FOUND, "data", 103),
20+
DOWNLOAD_NOT_FOUND(HttpStatus.NOT_FOUND, "data", 104),
21+
MESSAGE_GROUP_NOT_FOUND(HttpStatus.NOT_FOUND, "data", 105),
22+
23+
WRONG_FILE_TYPE(HttpStatus.BAD_REQUEST, "data", 2),
24+
FILE_TOO_LARGE(HttpStatus.BAD_REQUEST, "data", 3),
25+
PAGE_SIZE_TOO_LARGE(HttpStatus.BAD_REQUEST, "data", 4),
26+
REQUIRED_ARGUMENTS_MISSING(HttpStatus.BAD_REQUEST, "data", 5),
27+
28+
TOO_MANY_RESOURCE_CREATIONS(HttpStatus.TOO_MANY_REQUESTS, "resource", 1),
29+
TOO_MANY_RESOURCE_UPDATES(HttpStatus.TOO_MANY_REQUESTS, "resource", 2),
30+
RESOURCE_NAME_NOT_AVAILABLE(HttpStatus.BAD_REQUEST, "resource", 3);
31+
32+
private final HttpStatus httpStatus;
33+
private final String module;
34+
private final int errorCode;
35+
36+
RestErrorCode(HttpStatus httpStatus, String module, int errorCode) {
37+
this.httpStatus = httpStatus;
38+
this.module = module;
39+
this.errorCode = errorCode;
40+
}
41+
42+
@JsonIgnore
43+
public HttpStatus getHttpStatus() {
44+
return this.httpStatus;
45+
}
46+
47+
public String getModule() {
48+
return this.module;
49+
}
50+
51+
public int getErrorCode() {
52+
return this.errorCode;
53+
}
54+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.imjustdoom.pluginsite.config.exception;
2+
3+
import lombok.Getter;
4+
import org.springframework.lang.Nullable;
5+
6+
@Getter
7+
public class RestException extends Exception {
8+
private final RestErrorCode errorCode;
9+
private final @Nullable String message;
10+
11+
public RestException(RestErrorCode errorCode, String message, Object... params) {
12+
this.errorCode = errorCode;
13+
this.message = String.format(message, params);
14+
}
15+
16+
public RestException(RestErrorCode errorCode) {
17+
this.errorCode = errorCode;
18+
this.message = null;
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.imjustdoom.pluginsite.config.exception;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.RestControllerAdvice;
7+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
8+
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
@RestControllerAdvice
13+
@RequiredArgsConstructor
14+
public class RestExceptionResponseHandler extends ResponseEntityExceptionHandler {
15+
16+
private final ObjectMapper mapper;
17+
18+
@ExceptionHandler(RestException.class)
19+
public void handle(HttpServletResponse response, RestException exception) throws IOException {
20+
if (!response.isCommitted()) {
21+
response.setStatus(exception.getErrorCode().getHttpStatus().value());
22+
this.mapper.writeValue(response.getWriter(), exception);
23+
}
24+
}
25+
26+
// Could add an error ticketing system for unknown errors (5xx) thrown
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.imjustdoom.pluginsite.config.security;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
7+
import org.springframework.security.core.userdetails.UserDetailsService;
8+
import org.springframework.security.crypto.password.PasswordEncoder;
9+
10+
@Configuration
11+
@RequiredArgsConstructor
12+
public class AuthenticationProvider {
13+
private final UserDetailsService userDetailsService;
14+
private final PasswordEncoder passwordEncoder;
15+
16+
@Bean
17+
public DaoAuthenticationProvider authProvider() {
18+
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
19+
authProvider.setUserDetailsService(this.userDetailsService);
20+
authProvider.setPasswordEncoder(this.passwordEncoder);
21+
return authProvider;
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.imjustdoom.pluginsite.config.security;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
6+
import org.springframework.security.crypto.password.PasswordEncoder;
7+
8+
@Configuration
9+
public class PasswordEncoderConfig {
10+
11+
@Bean
12+
public PasswordEncoder encoder() {
13+
return new BCryptPasswordEncoder();
14+
}
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.imjustdoom.pluginsite.config.security;
2+
3+
import lombok.AllArgsConstructor;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
6+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
11+
@Configuration
12+
@EnableWebSecurity
13+
@AllArgsConstructor
14+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
15+
private final DaoAuthenticationProvider authProvider;
16+
17+
@Override
18+
protected void configure(AuthenticationManagerBuilder auth) {
19+
auth.authenticationProvider(this.authProvider);
20+
}
21+
22+
@Override
23+
public void configure(HttpSecurity http) throws Exception {
24+
http
25+
.csrf().disable()
26+
27+
.authorizeRequests()
28+
.antMatchers("/admin", "/admin/roles").hasRole("ADMIN")
29+
.antMatchers("/resources/create", "/account/details").authenticated()
30+
.antMatchers("/register", "/login").not().authenticated()
31+
32+
.anyRequest().permitAll()
33+
34+
.and()
35+
.formLogin().loginProcessingUrl("/login");
36+
}
37+
}

0 commit comments

Comments
 (0)