From a9c30d6ee1da3b47a28bd50490f53621cf17b8b0 Mon Sep 17 00:00:00 2001 From: "bakulkumar.kakadiya" Date: Thu, 6 Oct 2022 11:39:50 -0400 Subject: [PATCH] Updated to use H2 DB --- pom.xml | 6 +++--- .../springjwt/security/WebSecurityConfig.java | 18 +++++++----------- src/main/resources/application.properties | 17 +++++++++++------ src/main/resources/data.sql | 3 +++ 4 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 src/main/resources/data.sql diff --git a/pom.xml b/pom.xml index b39061d..b0e90ce 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ Demo project for Spring Boot Security - JWT - 1.8 + 11 @@ -41,8 +41,8 @@ - mysql - mysql-connector-java + com.h2database + h2 runtime diff --git a/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java b/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java index 3114f6b..adbe61f 100644 --- a/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java +++ b/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java @@ -30,18 +30,11 @@ public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter { @Autowired UserDetailsServiceImpl userDetailsService; - @Autowired - private AuthEntryPointJwt unauthorizedHandler; - @Bean public AuthTokenFilter authenticationJwtTokenFilter() { return new AuthTokenFilter(); } -// @Override -// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { -// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); -// } @Bean public DaoAuthenticationProvider authenticationProvider() { @@ -83,11 +76,14 @@ public PasswordEncoder passwordEncoder() { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http.cors().and().csrf().disable() - .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() + http + .headers().frameOptions().disable(). + and().cors().and().csrf().disable() + //.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() - .authorizeRequests().antMatchers("/api/auth/**").permitAll() - .antMatchers("/api/test/**").permitAll() + .authorizeRequests() + .antMatchers("/api/auth/**", "/error").permitAll() + .antMatchers("/api/test/**", "/h2-console/**").permitAll() .anyRequest().authenticated(); http.authenticationProvider(authenticationProvider()); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d02fe7a..0b24993 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,15 @@ -spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false -spring.datasource.username= root -spring.datasource.password= 123456 +spring.datasource.url=jdbc:h2:file:/data/demo +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password= +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.generate-ddl=true +spring.h2.console.enabled=true -spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect -spring.jpa.hibernate.ddl-auto= update +spring.jpa.defer-datasource-initialization=true # App Properties bezkoder.app.jwtSecret= bezKoderSecretKey -bezkoder.app.jwtExpirationMs= 86400000 \ No newline at end of file +bezkoder.app.jwtExpirationMs= 120000 + +logging.level.org.springframework=DEBUG \ No newline at end of file diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000..0c57de7 --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,3 @@ +INSERT INTO roles(name) VALUES('ROLE_USER'); +INSERT INTO roles(name) VALUES('ROLE_MODERATOR'); +INSERT INTO roles(name) VALUES('ROLE_ADMIN'); \ No newline at end of file