Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Import;
  5. import org.springframework.web.servlet.config.annotation.*;
  6. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  7. import org.springframework.web.servlet.view.JstlView;
  8.  
  9. @EnableWebMvc
  10. @Configuration
  11. @ComponentScan({ "de.dashboard.spring.web" })
  12. @Import({ SecurityConfig.class })
  13. public class AppConfig extends WebMvcConfigurerAdapter {
  14.  
  15. @Override
  16. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  17. configurer.enable();
  18. }
  19.  
  20. @Bean
  21. public InternalResourceViewResolver viewResolver() {
  22. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  23. viewResolver.setViewClass(JstlView.class);
  24. viewResolver.setPrefix("/pages/");
  25. viewResolver.setSuffix(".jsp");
  26. return viewResolver;
  27. }
  28. }
  29.  
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.context.annotation.Configuration;
  32. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  33. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  34. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  35. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  36. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  37.  
  38. @Configuration
  39. @EnableWebSecurity
  40. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  41.  
  42. @Autowired
  43. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  44. auth.inMemoryAuthentication().withUser("test").password("test").roles("USER");
  45. }
  46.  
  47. @Override
  48. protected void configure(HttpSecurity http) throws Exception {
  49. http.authorizeRequests()
  50. .antMatchers("/login").permitAll()
  51. .antMatchers("/**").access("hasRole('ROLE_USER')")
  52. .and()
  53. .formLogin().loginPage("/login")
  54. .failureUrl("/login?error")
  55. .usernameParameter("username").passwordParameter("password")
  56. .and()
  57. .csrf();
  58. }
  59. }
  60.  
  61. @Controller
  62. public class HelloController {
  63.  
  64. @RequestMapping(value = "/login", method = RequestMethod.GET)
  65. public String login(@RequestParam(value = "error", required = false) String error,
  66. @RequestParam(value = "logout", required = false) String logout,
  67. Model model) {
  68.  
  69. if (error != null) {
  70. model.addAttribute("error", "Invalid username and password!");
  71. }
  72.  
  73. if (logout != null) {
  74. model.addAttribute("msg", "You've been logged out successfully.");
  75. }
  76.  
  77. //Only as test (doesn't work)
  78. model.addAttribute("error","Test");
  79. return "login";
  80.  
  81. }
  82. }
  83.  
  84. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  85. <html>
  86. <head>
  87. <title>Login Page</title>
  88. <style>
  89. .error {
  90. padding: 15px;
  91. margin-bottom: 20px;
  92. border: 1px solid transparent;
  93. border-radius: 4px;
  94. color: #a94442;
  95. background-color: #f2dede;
  96. border-color: #ebccd1;
  97. }
  98.  
  99. .msg {
  100. padding: 15px;
  101. margin-bottom: 20px;
  102. border: 1px solid transparent;
  103. border-radius: 4px;
  104. color: #31708f;
  105. background-color: #d9edf7;
  106. border-color: #bce8f1;
  107. }
  108.  
  109. #login-box {
  110. width: 300px;
  111. padding: 20px;
  112. margin: 100px auto;
  113. background: #fff;
  114. -webkit-border-radius: 2px;
  115. -moz-border-radius: 2px;
  116. border: 1px solid #000;
  117. }
  118. </style>
  119. </head>
  120. <body onload='document.loginForm.username.focus();'>
  121.  
  122. <h1></h1>
  123.  
  124. <div id="login-box">
  125.  
  126. <h2>Login with Username and Password</h2>
  127.  
  128. <c:if test="${not empty error}">
  129. <div class="error">${error}</div>
  130. </c:if>
  131. <c:if test="${not empty msg}">
  132. <div class="msg">${msg}</div>
  133. </c:if>
  134.  
  135. <form name='loginForm'
  136. action="<c:url value='/login' />" method='POST'>
  137.  
  138. <table>
  139. <tr>
  140. <td>User:</td>
  141. <td><input type='text' name='username' value=''></td>
  142. </tr>
  143. <tr>
  144. <td>Password:</td>
  145. <td><input type='password' name='password' /></td>
  146. </tr>
  147. <tr>
  148. <td colspan='2'><input name="submit" type="submit"
  149. value="submit" /></td>
  150. </tr>
  151. </table>
  152.  
  153. <input type="hidden" name="${_csrf.parameterName}"
  154. value="${_csrf.token}" />
  155.  
  156. </form>
  157. </div>
  158.  
  159. </body>
  160. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement