Skip to content

Commit

Permalink
Change test database to H2
Browse files Browse the repository at this point in the history
  • Loading branch information
dhb52 authored and murraco committed May 29, 2020
1 parent 8d30add commit fefc24b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ build/
nbbuild/
dist/
nbdist/
.nb-gradle/
.nb-gradle/
.vscode/settings.json
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>murraco.JwtAuthServiceApp</start-class>
</properties>

<parent>
Expand Down Expand Up @@ -55,6 +56,12 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<!-- Automated JSON API documentation for API's built with Spring -->
<groupId>io.springfox</groupId>
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/murraco/security/JwtTokenFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean;

import murraco.exception.CustomException;

public class JwtTokenFilter extends GenericFilterBean {

private JwtTokenProvider jwtTokenProvider;
Expand All @@ -23,12 +26,19 @@ public JwtTokenFilter(JwtTokenProvider jwtTokenProvider) {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
throws IOException, ServletException {

String token = jwtTokenProvider.resolveToken((HttpServletRequest) req);
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication auth = token != null ? jwtTokenProvider.getAuthentication(token) : null;
SecurityContextHolder.getContext().setAuthentication(auth);
try {
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication auth = token != null ? jwtTokenProvider.getAuthentication(token) : null;
SecurityContextHolder.getContext().setAuthentication(auth);
}
} catch (CustomException ex) {
HttpServletResponse response = (HttpServletResponse) res;
response.sendError(ex.getHttpStatus().value(), ex.getMessage());
return;
}

filterChain.doFilter(req, res);
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/murraco/security/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()//
.antMatchers("/users/signin").permitAll()//
.antMatchers("/users/signup").permitAll()//
.antMatchers("/h2-console/**/**").permitAll()
// Disallow everything else..
.anyRequest().authenticated();

Expand All @@ -52,7 +53,12 @@ public void configure(WebSecurity web) throws Exception {
.antMatchers("/swagger-ui.html")//
.antMatchers("/configuration/**")//
.antMatchers("/webjars/**")//
.antMatchers("/public");
.antMatchers("/public")

// Un-secure H2 Database (for testing purposes, H2 console shouldn't be unprotected in production)
.and()
.ignoring()
.antMatchers("/h2-console/**/**");;
}

@Bean
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/user_db
username: root
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password: null
tomcat:
max-wait: 20000
Expand All @@ -13,7 +13,7 @@ spring:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
dialect: org.hibernate.dialect.H2Dialect
format_sql: true
id:
new_generator_mappings: false
Expand Down

0 comments on commit fefc24b

Please sign in to comment.