Guest User

Untitled

a guest
May 6th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. from passlib.context import CryptContext
  2.  
  3. # Encrypt the password
  4.  
  5. default_crypt_context = CryptContext(['pbkdf2_sha512', 'md5_crypt'],deprecated=['md5_crypt'],)
  6. encrypted = default_crypt_context.encrypt('password')
  7.  
  8. # Password verification can be done this way
  9. default_crypt_context.verify('password',encrypted)
  10.  
  11. @Bean
  12. public Pbkdf2PasswordEncoder passwordEncoder() {
  13.  
  14. Pbkdf2PasswordEncoder pbkdf2Sha512 = new Pbkdf2PasswordEncoder(secret, iterations, hashWidth);
  15. pbkdf2Sha512.setEncodeHashAsBase64(true);
  16. pbkdf2Sha512.setAlgorithm(SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA512);
  17. return pbkdf2Sha512;
  18. }
  19.  
  20. @PostConstruct
  21. public void init() {
  22. try {
  23. // auth.inMemoryAuthentication().withUser("julius").password("{noop}secret").roles("USER").and().withUser("admin")
  24. // .password("{noop}admin").roles("USER", "ADMIN");
  25. auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(
  26. "select login as username, password_crypt as password, active as enabled from res_users where login=?")
  27. .authoritiesByUsernameQuery("select ru.login as username, rg.name as authority "
  28. + "from res_groups_users_rel rgu, res_users ru, res_groups rg "
  29. + "where rg.id = rgu.gid and ru.id = rgu.uid and ru.login=?");
  30. // .passwordEncoder(new Pbkdf2PasswordEncoder());
  31.  
  32. } catch (Exception e) {
  33. throw new BeanInitializationException("Security configuration failed", e);
  34. }
  35. }
  36.  
  37. @Override
  38. public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
  39. throws AuthenticationException, IOException, ServletException {
  40. // Content-Type: application/x-www-form-urlencoded
  41. String username = request.getParameter("username");
  42. String password = request.getParameter("password");
  43. UsernamePasswordAuthenticationToken authenticationToken =
  44. new UsernamePasswordAuthenticationToken(username, password);
  45.  
  46. Authentication authentication = this.authenticationManager.authenticate(authenticationToken);
  47. SecurityContextHolder.getContext().setAuthentication(authentication);
  48. return authentication;
  49. }
  50.  
  51. $pbkdf2-sha512$25000$vpfyXqs1xhgDIMTY21vLuQ$DAKoL7puHkSZeQM0dF/caP6rbuhXSoIKAMue5a2jcCAf8.P62lJUKy9CDfjCyvXhLqfxUXukvp7LBrL8zZok8g
  52.  
  53. ERROR o.s.b.w.s.support.ErrorPageFilter - Forwarding to error page from request [/api/authenticate] due to exception [Detected a Non-hex character at 1 or 2 position]
  54. java.lang.IllegalArgumentException: Detected a Non-hex character at 1 or 2 position
  55. at org.springframework.security.crypto.codec.Hex.decode(Hex.java:62)
  56. at org.springframework.security.crypto.password.Pbkdf2PasswordEncoder.decode(Pbkdf2PasswordEncoder.java:166)
  57. at org.springframework.security.crypto.password.Pbkdf2PasswordEncoder.matches(Pbkdf2PasswordEncoder.java:142)
  58. at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$LazyPasswordEncoder.matches(AuthenticationConfiguration.java:289)
  59. at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:86)
  60. at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166)
  61. at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
Add Comment
Please, Sign In to add comment