Skip to content

Instantly share code, notes, and snippets.

View jeffersonchaves's full-sized avatar
😎
Focusing

Jefferson Chaves jeffersonchaves

😎
Focusing
  • Instituto Federal do Paraná - IFPR
  • Foz do Iguaçu - PR
View GitHub Profile
#spring.profiles.active=prod
spring.datasource.url=jdbc:postgresql://dpg-d1hccdmr433s738rs3eg-a:5432/restlab
spring.datasource.username=admin
spring.datasource.password=I4OND2VYDnHjZh272zMevmW5jVyq37ni
spring.jpa.hibernate.ddl-auto=update
# ddl-auto: permite criação e atualização das tabelas do banco;
@Service
public class AuthenticationService {
@Autowired
JwtService jwtService;
public String authenticate(Authentication authentication){
return jwtService.generateToken(authentication);
}
@RestController
public class AuthenticationResource {
@Autowired
private AuthenticationService authenticationService;
@PostMapping("/authenticate")
public String authenticate(Authentication authentication){
return authenticationService.authenticate(authentication);
}
INSERT INTO user (`password`, `username`) VALUES ('senha_criptografada', 'username');
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3FlqJr5TRskIQIgdE3Dd
7D9lboWdcTUT8a+fJR7MAvQm7XXNoYkm3v7MQL1NYtDvL2l8CAnc0WdSTINU6IRv
c5Kqo2Q4csNX9SHOmEfzoROjQqahEcve1jBXluoCXdYuYpx4/1tfRgG6ii4Uhxh6
iI8qNMJQX+fLfqhbfYfxBQVRPywBkAbIP4x1EAsbC6FSNmkhCxiMNqEgxaIpY8C2
kJdJ/ZIV+WW4noDdzpKqHcwmB8FsrumlVY/DNVvUSDIipiq9PbP4H99TXN1o746o
RaNa07rq1hoCgMSSy+85SagCoxlmyE+D+of9SsMY8Ol9t0rdzpobBuhyJ/o5dfvj
KwIDAQAB
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDcWWomvlNGyQhA
iB0TcN3sP2VuhZ1xNRPxr58lHswC9Cbtdc2hiSbe/sxAvU1i0O8vaXwICdzRZ1JM
g1TohG9zkqqjZDhyw1f1Ic6YR/OhE6NCpqERy97WMFeW6gJd1i5inHj/W19GAbqK
LhSHGHqIjyo0wlBf58t+qFt9h/EFBVE/LAGQBsg/jHUQCxsLoVI2aSELGIw2oSDF
oiljwLaQl0n9khX5ZbiegN3OkqodzCYHwWyu6aVVj8M1W9RIMiKmKr09s/gf31Nc
3WjvjqhFo1rTuurWGgKAxJLL7zlJqAKjGWbIT4P6h/1Kwxjw6X23St3OmhsG6HIn
+jl1++MrAgMBAAECggEBAMf820wop3pyUOwI3aLcaH7YFx5VZMzvqJdNlvpg1jbE
E2Sn66b1zPLNfOIxLcBG8x8r9Ody1Bi2Vsqc0/5o3KKfdgHvnxAB3Z3dPh2WCDek
lCOVClEVoLzziTuuTdGO5/CWJXdWHcVzIjPxmK34eJXioiLaTYqN3XKqKMdpD0ZG
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Value("${jwt.public.key}")
RSAPublicKey publicKey;
@Value("${jwt.private.key}")
RSAPrivateKey privateKey;
@Service
public class JwtService {
@Autowired
private JwtEncoder encoder;
public String generateToken(Authentication authentication) {
Instant now = Instant.now();
long expiry = 525600L;
@Service
public class UserDetailService implements UserDetailsService {
@Autowired
UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
public class UserAuthenticated implements UserDetails{
User user;
public UserAuthenticated(User user) {
this.user = user;
}
@Override