auth pass hash
parent
9a886059ca
commit
6274a83b95
|
@ -0,0 +1,17 @@
|
||||||
|
package ba.steleks;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by admin on 13/05/2017.
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class UsersConfig {
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder providePasswordEncoder(){
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package ba.steleks.controller;
|
||||||
|
|
||||||
|
import ba.steleks.model.AuthRequest;
|
||||||
|
import ba.steleks.repository.UsersJpaRepository;
|
||||||
|
import org.bouncycastle.crypto.tls.HashAlgorithm;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by admin on 13/05/2017.
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class AuthenticationController {
|
||||||
|
|
||||||
|
private UsersJpaRepository usersJpaRepository;
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public AuthenticationController(UsersJpaRepository usersJpaRepository, PasswordEncoder passwordEncoder) {
|
||||||
|
this.passwordEncoder=passwordEncoder;
|
||||||
|
this.usersJpaRepository = usersJpaRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/accesstoken" , method = RequestMethod.POST)
|
||||||
|
public String generateToken(@RequestBody AuthRequest body){
|
||||||
|
return passwordEncoder.matches(body.getPassword(),usersJpaRepository.findByUsername(body.getUsername()).getPasswordHash()) ? "true" : "false";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package ba.steleks.model;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by admin on 13/05/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AuthRequest {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public AuthRequest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthRequest(String username, String password) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -8,6 +8,6 @@ import ba.steleks.model.User;
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
|
||||||
public interface UsersJpaRepository extends PagingAndSortingRepository<User, Long> {
|
public interface UsersJpaRepository extends PagingAndSortingRepository<User, Long> {
|
||||||
|
User findByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue