Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <!--?xml version="1.0" encoding="UTF-8"?-->
  2. <beans:beans xmlns="http://www.springframework.org/schema/security"
  3. xmlns:beans="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/security
  8. http://www.springframework.org/schema/security/spring-security.xsd">
  9.  
  10. <http create-session="always"
  11. use-expressions="true"
  12. authentication-manager-ref="authenticationManager"
  13. entry-point-ref="authenticationEntryPoint">
  14. <csrf disabled="true" />
  15. <intercept-url pattern="/**" access="hasRole('USER')" />
  16. <form-login authentication-success-handler-ref="customAuthenticationSuccessHandler" />
  17. <logout />
  18. </http>
  19.  
  20. <authentication-manager alias="authenticationManager">
  21. <authentication-provider user-service-ref="userDao"></authentication-provider>
  22. </authentication-manager>
  23.  
  24. </beans:beans>
  25.  
  26. @Component
  27. public class CustomEntryPoint implements AuthenticationEntryPoint {
  28.  
  29. @Override
  30. public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
  31. System.out.println("Entering commence due to failed Authentication");
  32. response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized Access!");
  33. }
  34.  
  35. }
  36.  
  37. public class UserDao implements UserDetailsService {
  38.  
  39. @Override
  40. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  41.  
  42. String password = readPasswordFromFileOrDatabase(username);
  43.  
  44. if (password == null) throw new UsernameNotFoundException("failure");
  45. return User.withUsername("user").password(password).authorities("ROLE_USER").build();
  46. }
  47.  
  48. private String readPasswordFromFileOrDatabase(String username) {
  49. if (username.equals("user")) return "q";
  50. return null;
  51. }
  52.  
  53.  
  54. }
  55.  
  56. String plainClientCredentials="myusername:mypassword";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement