Guest User

Untitled

a guest
Feb 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. package com.project.springinventory.configuration;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  9. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  10. import org.springframework.security.core.userdetails.UserDetailsService;
  11. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  12.  
  13. @Configuration
  14. @EnableWebSecurity
  15. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  16.  
  17. @Autowired
  18. @Qualifier("UsersService")
  19. private UserDetailsService userService;
  20.  
  21. @Autowired
  22. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  23. auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
  24. }
  25.  
  26.  
  27.  
  28. @Override
  29. protected void configure(HttpSecurity http) throws Exception {
  30. // TODO Auto-generated method stub
  31. http.authorizeRequests().antMatchers("/css/*,/imgs/*").permitAll().and().formLogin().loginPage("/login").loginProcessingUrl("/loginCheck").usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/customermanagement").permitAll().and().logout().logoutUrl("/logout").permitAll();
  32. super.configure(http);
  33. }
  34.  
  35.  
  36.  
  37. }
  38.  
  39. package com.project.springinventory.controller;
  40.  
  41. import org.springframework.stereotype.Controller;
  42. import org.springframework.web.bind.annotation.GetMapping;
  43. import org.springframework.web.bind.annotation.ModelAttribute;
  44. import org.springframework.web.bind.annotation.PostMapping;
  45. import org.springframework.web.bind.annotation.RequestMapping;
  46. import org.springframework.web.servlet.ModelAndView;
  47. import com.project.springinventory.constant.ViewConstant;
  48. import com.project.springinventory.entity.User;
  49.  
  50. @Controller
  51. @RequestMapping("/")
  52.  
  53. public class MainIndexController {
  54.  
  55. @GetMapping("/")
  56. public ModelAndView ViewPost(@ModelAttribute("EntityUser") User entityUser) {
  57. ModelAndView mav = new ModelAndView();
  58.  
  59. // @Valid @ModelAttribute("EntityUser") User entityUser ,BindingResult
  60. // bindingResult
  61.  
  62. // System.err.println("User: " + entityUser.getUsername() + " Pass: " +
  63. // entityUser.getPassword());
  64.  
  65. // if (bindingResult.hasErrors()) {
  66. // mav.setViewName(ViewConstant.LOGINERROR_VIEW);
  67. // } else {
  68.  
  69. // if (entityUser.getUsername().equalsIgnoreCase("admin") &&
  70. // entityUser.getPassword().equalsIgnoreCase("admin")) {
  71. // ViewConstant.REDIRECT_CONSTANT +
  72. mav.setViewName(ViewConstant.LOGIN_VIEW);
  73. // mav.addObject("EntityUser", entityUser);
  74.  
  75. // } else {
  76. // mav.setViewName(ViewConstant.LOGINERROR_VIEW);
  77. // mav.addObject("EntityUser", new User());
  78. // }
  79. // }
  80.  
  81. return mav;
  82. }
  83.  
  84. }
  85.  
  86. <!DOCTYPE html>
  87. <html lang="en-US" xmlns:th="http://www.thymeleaf.org">
  88.  
  89. <head>
  90.  
  91. <meta charset="UTF-8" />
  92.  
  93. <title>Acceso</title>
  94.  
  95.  
  96.  
  97. </head>
  98.  
  99. <body>
  100.  
  101.  
  102. <div class="login-card">
  103. <h1>
  104. <span th:text="${name}"></span> <img class="candado"
  105. th:src="@{/img/candado.png}" src="/static/img/candado.png" />
  106. </h1>
  107. <br />
  108.  
  109. <form th:action="@{/login}"
  110. method="POST">
  111. <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
  112.  
  113. <input type="text" name="username" placeholder="Username" id="username"/>
  114. <!-- <p th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Username -->
  115. <!-- has errors</p> -->
  116. <input type="password" name="pass" placeholder="Password" id="password"/>
  117. <!-- <p th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Password -->
  118. <!-- has errors</p> -->
  119. <input type="submit" name="login" class="btn"
  120. value="Acceder" />
  121. </form>
  122.  
  123. <div class="login-help">
  124. <a href="#">Forgot Password</a>
  125. </div>
  126. </div>
  127.  
  128.  
  129. </body>
  130.  
  131. <link rel="stylesheet" th:href="@{/css/jquery-ui.css}"
  132. href="/static/css/jquery-ui.css" />
  133.  
  134. <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
  135. href="/static/css/bootstrap.min.css" />
  136.  
  137. <link rel="stylesheet" th:href="@{/css/style.css}"
  138. href="/static/css/style.css" />
  139.  
  140. </html>
Add Comment
Please, Sign In to add comment