Created
September 14, 2023 06:12
-
-
Save mayankchoubey/f4651d52c7d7e2c3a1f28da668c15c19 to your computer and use it in GitHub Desktop.
SpringBoot - JWT verify and MySQL read
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.port=3000 | |
| spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false&allowPublicKeyRetrieval=true | |
| spring.datasource.username= dbuser | |
| spring.datasource.password= dbpwd | |
| spring.jpa.hibernate.ddl-auto= update | |
| spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | |
| spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.demo; | |
| import jakarta.persistence.Entity; | |
| import jakarta.persistence.Table; | |
| import jakarta.persistence.GeneratedValue; | |
| import jakarta.persistence.GenerationType; | |
| import jakarta.persistence.Id; | |
| @Entity | |
| @Table(name = "users") | |
| public class User { | |
| @Id | |
| private String email; | |
| private String first; | |
| private String last; | |
| private String city; | |
| private String county; | |
| private int age; | |
| public String getId() { | |
| return email; | |
| } | |
| public void setId(String email) { | |
| this.email = email; | |
| } | |
| public String getFirst() { | |
| return first; | |
| } | |
| public void setFirst(String name) { | |
| this.first = name; | |
| } | |
| public String getLast() { | |
| return last; | |
| } | |
| public void setLast(String name) { | |
| this.last = name; | |
| } | |
| public String getEmail() { | |
| return email; | |
| } | |
| public void setEmail(String email) { | |
| this.email = email; | |
| } | |
| public String getCity() { | |
| return city; | |
| } | |
| public void setCity(String city) { | |
| this.city = city; | |
| } | |
| public String getCounty() { | |
| return county; | |
| } | |
| public void setCounty(String county) { | |
| this.county = county; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.demo; | |
| import org.springframework.boot.SpringApplication; | |
| import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer; | |
| import org.springframework.context.annotation.Bean; | |
| @SpringBootApplication | |
| public class UserApplication { | |
| public static void main(String[] args) { | |
| SpringApplication.run(UserApplication.class, args); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.demo; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.RequestHeader; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.http.HttpStatus; | |
| import org.springframework.http.HttpHeaders; | |
| import org.springframework.web.bind.annotation.RestController; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import java.util.Optional; | |
| import io.jsonwebtoken.Jwts; | |
| import io.jsonwebtoken.Jws; | |
| import io.jsonwebtoken.Claims; | |
| import io.jsonwebtoken.SignatureAlgorithm; | |
| import io.jsonwebtoken.security.Keys; | |
| import java.security.Key; | |
| import com.example.demo.UserRepository; | |
| import com.example.demo.User; | |
| @RestController | |
| public class UserController { | |
| @Autowired | |
| UserRepository userRepository; | |
| private SignatureAlgorithm sa = SignatureAlgorithm.HS256; | |
| private String jwtSecret = System.getenv("JWT_SECRET"); | |
| @GetMapping("/") | |
| public User handleRequest(@RequestHeader(HttpHeaders.AUTHORIZATION) String authHdr) { | |
| String jwtString = authHdr.replace("Bearer",""); | |
| Claims claims = Jwts.parser() | |
| .setSigningKey(jwtSecret.getBytes()) | |
| .parseClaimsJws(jwtString).getBody(); | |
| Optional<User> user = userRepository.findById((String)claims.get("email")); | |
| return user.get(); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.demo; | |
| import org.springframework.data.repository.CrudRepository; | |
| import com.example.demo.User; | |
| public interface UserRepository extends CrudRepository<User, String> { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment