Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. @EnableGlobalMethodSecurity(prePostEnabled = true)
  2. @EnableWebSecurity
  3. @EnableJpaRepositories(basePackageClasses = UserRepository.class)
  4. @Configuration
  5. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  6.  
  7.  
  8. @Autowired
  9. private CustomUserDetailsService userDetailsService;
  10.  
  11. @Override
  12. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  13.  
  14. auth.userDetailsService(userDetailsService)
  15. .passwordEncoder(getPasswordEncoder());
  16. }
  17.  
  18.  
  19. @Override
  20. protected void configure(HttpSecurity http) throws Exception {
  21.  
  22. http.csrf().disable();
  23. http.authorizeRequests()
  24. .antMatchers("**/secured/**").authenticated()
  25. .anyRequest().permitAll()
  26. .and()
  27. .formLogin().loginPage("/loginPage")
  28. .usernameParameter("username").passwordParameter("password")
  29. .permitAll();
  30.  
  31.  
  32.  
  33. }
  34.  
  35. private PasswordEncoder getPasswordEncoder() {
  36. return new PasswordEncoder() {
  37. @Override
  38. public String encode(CharSequence charSequence) {
  39. return charSequence.toString();
  40. }
  41.  
  42. @Override
  43. public boolean matches(CharSequence charSequence, String s) {
  44. return true;
  45. }
  46. };
  47. }
  48. }
  49.  
  50. public class WelcomeController {
  51.  
  52. @Autowired
  53. private UserRepository userRepository;
  54.  
  55. @RequestMapping("/w")
  56. public String welcome(ModelMap map) {
  57. map.put("currentDate", new Date());
  58. return "welcome";
  59. }
  60.  
  61. @RequestMapping("/loginPage")
  62. public String getLoginPage(ModelMap map) {
  63.  
  64. System.out.println("geberrich loginpage");
  65.  
  66. return "loginPage";
  67. }
  68.  
  69.  
  70. @RequestMapping(value= "/add",method= RequestMethod.GET) // Map ONLY GET Requests
  71. public @ResponseBody String addNewUser (@RequestParam(value="t1") String name
  72. , @RequestParam(value="t2") String email,@RequestParam(value="t3") String password) {
  73. // @ResponseBody means the returned String is the response, not a view name
  74. // @RequestParam means it is a parameter from the GET or POST request
  75.  
  76. //build roles
  77. Set roles= new HashSet<Role>();
  78. Role role= new Role();
  79. role.setRole("ADMI");
  80. roles.add(role);
  81. //add user
  82. User n = new User();
  83. n.setName(name);
  84. n.setEmail(email);
  85. n.setPassword(password);
  86. n.setRoles(roles);
  87. userRepository.save(n);
  88. return "Saved";
  89. }
  90.  
  91. @PreAuthorize("hasAnyRole('ADMI')")
  92. @RequestMapping(value= "/secured/all",method= RequestMethod.GET)
  93. public @ResponseBody String securedHello() {
  94. System.out.println("geberrich secured all");
  95. return "Secured Hello";
  96. }
  97.  
  98. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  99. pageEncoding="ISO-8859-1"%>
  100. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  101. <html>
  102. <head>
  103. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  104. <title>Insert title here</title>
  105. </head>
  106. <body>
  107. <h3>Login with user name and password</h3>
  108. <form >
  109. <label for="username">Username</label>
  110. <input type="text" name="username" id="username"><br>
  111. <label for="password">Password</label>
  112. <input type="password" id="password" name="password"/><br>
  113. <button type="submit"> Login</button><br>
  114.  
  115. </body>
  116. </html>
  117.  
  118. 2017-12-18 13:16:50.372 WARN 5804 --- [nio-8082-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [int]; nested exception is java.lang.NumberFormatException: For input string: "robots"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement