Skip to content

refactor #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.bezkoder</groupId>
<artifactId>spring-boot-security-jwt</artifactId>
<groupId>com.example</groupId>
<artifactId>spring-boot-spring-security-jwt-authentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-security-jwt</name>
<description>Demo project for Spring Boot Security - JWT</description>
Expand All @@ -35,11 +35,11 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>io.jsonwebtoken</groupId>
Expand All @@ -52,7 +52,17 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
Expand Down

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/java/com/bezkoder/springjwt/models/Role.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

38 changes: 38 additions & 0 deletions src/main/java/com/example/SpringBootSecurityJwtApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example;

import com.example.models.ERole;
import com.example.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import com.example.models.Role;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootSecurityJwtApplication implements ApplicationRunner {

public static void main(String[] args) {
SpringApplication.run(SpringBootSecurityJwtApplication.class, args);
}

@Autowired
private RoleRepository roleRepository;

@Override
public void run(ApplicationArguments args) throws Exception {
Role role1 = new Role();
role1.setName(ERole.ROLE_USER);

Role role2 = new Role();
role2.setName(ERole.ROLE_ADMIN);

Role role3 = new Role();
role3.setName(ERole.ROLE_MODERATOR);

roleRepository.save(role1);
roleRepository.save(role2);
roleRepository.save(role3);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bezkoder.springjwt.controllers;
package com.example.controllers;

import java.util.HashSet;
import java.util.List;
Expand All @@ -7,6 +7,16 @@

import javax.validation.Valid;

import com.example.models.ERole;
import com.example.models.Role;
import com.example.models.User;
import com.example.payload.request.LoginRequest;
import com.example.payload.request.SignupRequest;
import com.example.payload.response.JwtResponse;
import com.example.payload.response.MessageResponse;
import com.example.repository.UserRepository;
import com.example.security.jwt.JwtUtils;
import com.example.security.services.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -20,17 +30,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bezkoder.springjwt.models.ERole;
import com.bezkoder.springjwt.models.Role;
import com.bezkoder.springjwt.models.User;
import com.bezkoder.springjwt.payload.request.LoginRequest;
import com.bezkoder.springjwt.payload.request.SignupRequest;
import com.bezkoder.springjwt.payload.response.JwtResponse;
import com.bezkoder.springjwt.payload.response.MessageResponse;
import com.bezkoder.springjwt.repository.RoleRepository;
import com.bezkoder.springjwt.repository.UserRepository;
import com.bezkoder.springjwt.security.jwt.JwtUtils;
import com.bezkoder.springjwt.security.services.UserDetailsImpl;
import com.example.repository.RoleRepository;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
Expand Down Expand Up @@ -60,12 +60,12 @@ public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest login
SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtUtils.generateJwtToken(authentication);

UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
List<String> roles = userDetails.getAuthorities().stream()
.map(item -> item.getAuthority())
.collect(Collectors.toList());

return ResponseEntity.ok(new JwtResponse(jwt,
return ResponseEntity.ok(new JwtResponse(jwt,
userDetails.getId(),
userDetails.getUsername(),
userDetails.getEmail(),
Expand All @@ -87,7 +87,7 @@ public ResponseEntity<?> registerUser(@Valid @RequestBody SignupRequest signUpRe
}

// Create new user's account
User user = new User(signUpRequest.getUsername(),
User user = new User(signUpRequest.getUsername(),
signUpRequest.getEmail(),
encoder.encode(signUpRequest.getPassword()));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bezkoder.springjwt.controllers;
package com.example.controllers;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bezkoder.springjwt.models;
package com.example.models;

public enum ERole {
ROLE_USER,
Expand Down
Loading