auth pass hash

master
dizda13 2017-05-16 20:58:20 +02:00
parent 9a886059ca
commit 6274a83b95
4 changed files with 92 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

@ -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);
} }