Stash
This commit is contained in:
parent
43caf76471
commit
b82bab1f8e
@ -3,6 +3,7 @@ package hhn.labsw.bugageocaching.controller;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import hhn.labsw.bugageocaching.entities.*;
|
import hhn.labsw.bugageocaching.entities.*;
|
||||||
import hhn.labsw.bugageocaching.repositories.*;
|
import hhn.labsw.bugageocaching.repositories.*;
|
||||||
|
import hhn.labsw.bugageocaching.util.VerificationUtil;
|
||||||
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Claims;
|
||||||
import io.jsonwebtoken.ExpiredJwtException;
|
import io.jsonwebtoken.ExpiredJwtException;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
@ -13,7 +14,6 @@ import org.springframework.security.crypto.bcrypt.BCrypt;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.security.Key;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil;
|
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil;
|
||||||
@ -44,11 +44,10 @@ public class Controller {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
Key key;
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
key = fetchPublicKey();
|
fetchPublicKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@ -78,12 +77,12 @@ public class Controller {
|
|||||||
.setSubject(user.getUsername())
|
.setSubject(user.getUsername())
|
||||||
.claim("admin", userRepository.findByUsername(user.getUsername()).getRoles().stream().anyMatch(x -> x.getId() == 0)) //True if user is admin
|
.claim("admin", userRepository.findByUsername(user.getUsername()).getRoles().stream().anyMatch(x -> x.getId() == 0)) //True if user is admin
|
||||||
.setExpiration(new Date(new Date().getTime() + (1000 * 60 * 60 * 24))) //One day expiration
|
.setExpiration(new Date(new Date().getTime() + (1000 * 60 * 60 * 24))) //One day expiration
|
||||||
.signWith(signatureAlgorithm, key)
|
.signWith(signatureAlgorithm, VerificationUtil.publicKey)
|
||||||
.compact();
|
.compact();
|
||||||
System.out.println(token);
|
System.out.println(token);
|
||||||
|
|
||||||
Claims claims = Jwts.parser() //Parse JWT
|
Claims claims = Jwts.parser() //Parse JWT
|
||||||
.setSigningKey(key)
|
.setSigningKey(VerificationUtil.publicKey)
|
||||||
.parseClaimsJws(token).getBody();
|
.parseClaimsJws(token).getBody();
|
||||||
System.out.println("ID: " + claims.getId());
|
System.out.println("ID: " + claims.getId());
|
||||||
System.out.println("Subject: " + claims.getSubject());
|
System.out.println("Subject: " + claims.getSubject());
|
||||||
@ -109,7 +108,7 @@ public class Controller {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Claims claims = Jwts.parser() //Parse JWT
|
Claims claims = Jwts.parser() //Parse JWT
|
||||||
.setSigningKey(key)
|
.setSigningKey(VerificationUtil.publicKey)
|
||||||
.parseClaimsJws(token).getBody();
|
.parseClaimsJws(token).getBody();
|
||||||
|
|
||||||
User user = userRepository.findByUsername(claims.getSubject());
|
User user = userRepository.findByUsername(claims.getSubject());
|
||||||
@ -179,7 +178,7 @@ public class Controller {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Claims claims = Jwts.parser() //Parse JWT
|
Claims claims = Jwts.parser() //Parse JWT
|
||||||
.setSigningKey(key)
|
.setSigningKey(VerificationUtil.publicKey)
|
||||||
.parseClaimsJws(token).getBody();
|
.parseClaimsJws(token).getBody();
|
||||||
|
|
||||||
return ResponseEntity.status(200).body(claims.get("admin"));
|
return ResponseEntity.status(200).body(claims.get("admin"));
|
||||||
@ -212,7 +211,7 @@ public class Controller {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
Claims claims = Jwts.parser() //Parse JWT
|
Claims claims = Jwts.parser() //Parse JWT
|
||||||
.setSigningKey(key)
|
.setSigningKey(VerificationUtil.publicKey)
|
||||||
.parseClaimsJws(token).getBody();
|
.parseClaimsJws(token).getBody();
|
||||||
|
|
||||||
|
|
||||||
@ -248,7 +247,7 @@ public class Controller {
|
|||||||
User u = new User();
|
User u = new User();
|
||||||
u.setId((int) obj[0]);
|
u.setId((int) obj[0]);
|
||||||
u.setUsername((String) obj[1]);
|
u.setUsername((String) obj[1]);
|
||||||
u.setRankingPointsSum((int) obj[2]);
|
//u.setRankingPointsSum((int) obj[2]);
|
||||||
sendBackUsers.add(u);
|
sendBackUsers.add(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +260,7 @@ public class Controller {
|
|||||||
public ResponseEntity getUser(@RequestParam String token) {
|
public ResponseEntity getUser(@RequestParam String token) {
|
||||||
try {
|
try {
|
||||||
Claims claims = Jwts.parser() //Parse JWT
|
Claims claims = Jwts.parser() //Parse JWT
|
||||||
.setSigningKey(key)
|
.setSigningKey(VerificationUtil.publicKey)
|
||||||
.parseClaimsJws(token).getBody();
|
.parseClaimsJws(token).getBody();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@ public class Role {
|
|||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
|
private String domain;
|
||||||
|
|
||||||
public Role() {
|
public Role() {
|
||||||
|
|
||||||
@ -32,6 +33,14 @@ public class Role {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomain(String domain) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
|||||||
@ -15,10 +15,8 @@ public class User {
|
|||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String firstname;
|
|
||||||
private String lastname;
|
|
||||||
private String username;
|
private String username;
|
||||||
private int rankingPointsSum;
|
|
||||||
private String email;
|
private String email;
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@ -26,10 +24,6 @@ public class User {
|
|||||||
@ManyToMany
|
@ManyToMany
|
||||||
private List<Role> roles;
|
private List<Role> roles;
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
private Team team;
|
|
||||||
|
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
private String passwordConfirm;
|
private String passwordConfirm;
|
||||||
|
|
||||||
@ -41,22 +35,6 @@ public class User {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstname() {
|
|
||||||
return firstname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFirstname(String firstname) {
|
|
||||||
this.firstname = firstname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastname() {
|
|
||||||
return lastname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastname(String lastname) {
|
|
||||||
this.lastname = lastname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
@ -65,14 +43,6 @@ public class User {
|
|||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRankingPointsSum() {
|
|
||||||
return rankingPointsSum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRankingPointsSum(int rankingPointsSum) {
|
|
||||||
this.rankingPointsSum = rankingPointsSum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
@ -89,14 +59,6 @@ public class User {
|
|||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Team getTeam() {
|
|
||||||
return team;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTeam(Team team) {
|
|
||||||
this.team = team;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Role> getRoles() {
|
public List<Role> getRoles() {
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,46 @@
|
|||||||
|
package hhn.labsw.bugageocaching.entities;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table
|
||||||
|
public class User_Info implements Serializable {
|
||||||
|
|
||||||
|
@OneToOne
|
||||||
|
@Id
|
||||||
|
private User userID;
|
||||||
|
|
||||||
|
private int rankingPointsSum;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Team teamID;
|
||||||
|
|
||||||
|
|
||||||
|
public User_Info() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getUserID() {
|
||||||
|
return userID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserID(User userID) {
|
||||||
|
this.userID = userID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRankingPointsSum() {
|
||||||
|
return rankingPointsSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRankingPointsSum(int rankingPointsSum) {
|
||||||
|
this.rankingPointsSum = rankingPointsSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Team getTeamID() {
|
||||||
|
return teamID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTeamID(Team teamID) {
|
||||||
|
this.teamID = teamID;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,8 @@
|
|||||||
package hhn.labsw.bugageocaching.util;
|
package hhn.labsw.bugageocaching.util;
|
||||||
|
|
||||||
import hhn.labsw.bugageocaching.fetchObjects.PublicKey;
|
import hhn.labsw.bugageocaching.fetchObjects.PublicKey;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.security.Key;
|
import java.security.Key;
|
||||||
@ -10,7 +12,9 @@ import java.util.Base64;
|
|||||||
|
|
||||||
public class VerificationUtil {
|
public class VerificationUtil {
|
||||||
|
|
||||||
public static Key fetchPublicKey() {
|
public static Key publicKey;
|
||||||
|
|
||||||
|
public static void fetchPublicKey() {
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
try {
|
try {
|
||||||
PublicKey response = restTemplate.getForObject("http://seserver.se.hs-heilbronn.de:8090/buga19usermanagement/token/publickey", PublicKey.class);
|
PublicKey response = restTemplate.getForObject("http://seserver.se.hs-heilbronn.de:8090/buga19usermanagement/token/publickey", PublicKey.class);
|
||||||
@ -19,16 +23,13 @@ public class VerificationUtil {
|
|||||||
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodedKey);
|
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodedKey);
|
||||||
Key key = factory.generatePublic(publicKeySpec);
|
Key key = factory.generatePublic(publicKeySpec);
|
||||||
|
|
||||||
return key;
|
publicKey = key;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Fehler muss zurückgegeben werden
|
//Fehler muss zurückgegeben werden
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void verifyToken() {
|
//Verify methode
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user