Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:beans="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <annotation-driven />
  11.  
  12. static resources in the ${webappRoot}/resources directory -->
  13. <resources mapping="/resources/**" location="/resources/" />
  14.  
  15. <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <beans:property name="prefix" value="/WEB-INF/views/" />
  17. <beans:property name="suffix" value=".jsp" />
  18. </beans:bean>
  19.  
  20. <context:component-scan base-package="com.at.ccts" />
  21. </beans:beans>
  22.  
  23. <beans:beans xmlns="http://www.springframework.org/schema/security"
  24. xmlns:beans="http://www.springframework.org/schema/beans"
  25. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26. xsi:schemaLocation="http://www.springframework.org/schema/beans
  27. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  28. http://www.springframework.org/schema/security
  29. http://www.springframework.org/schema/security/spring-security-3.2.xsd">
  30.  
  31. <http auto-config="true">
  32. <intercept-url pattern="/admin**" access="ROLE_USER" />
  33.  
  34. <form-login
  35. login-page="/login"
  36. login-processing-url="/j_spring_security_check"
  37. authentication-failure-url="/login?error"
  38. username-parameter="username"
  39. password-parameter="password" />
  40. <logout logout-success-url="/login?logout" />
  41. <!-- enable csrf protection -->
  42. <csrf/>
  43. </http>
  44.  
  45. <authentication-manager>
  46. <authentication-provider>
  47. <user-service>
  48. <user name="admin" password="admin" authorities="ROLE_USER" />
  49. </user-service>
  50. </authentication-provider>
  51. </authentication-manager>
  52.  
  53. </beans:beans>
  54.  
  55. package com.at.ccts.controller;
  56.  
  57. import org.springframework.stereotype.Controller;
  58. import org.springframework.web.bind.annotation.RequestMapping;
  59. import org.springframework.web.bind.annotation.RequestMethod;
  60. import org.springframework.web.bind.annotation.RequestParam;
  61. import org.springframework.web.servlet.ModelAndView;
  62.  
  63. @Controller
  64. public class HomeController {
  65.  
  66. @RequestMapping(value = "/admin**", method = RequestMethod.GET)
  67. public ModelAndView adminPage() {
  68.  
  69. ModelAndView model = new ModelAndView();
  70. model.addObject("title", "Spring Security Custom Login Form");
  71. model.addObject("message", "This is protected page!");
  72. model.setViewName("admin");
  73.  
  74. return model;
  75. }
  76.  
  77. @RequestMapping(value = "/login", method = RequestMethod.GET)
  78. public ModelAndView login(@RequestParam(value = "error", required = false) String error,
  79. @RequestParam(value = "logout", required = false) String logout) {
  80.  
  81. ModelAndView model = new ModelAndView();
  82. if (error != null) {
  83. model.addObject("error", "Invalid username and password!");
  84. }
  85.  
  86. if (logout != null) {
  87. model.addObject("msg", "You've been logged out successfully.");
  88. }
  89. model.setViewName("login");
  90.  
  91. return model;
  92. }
  93. }
  94.  
  95. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  96. <%@page session="true"%>
  97. <html>
  98. <head>
  99. <title>Login Page</title>
  100. </head>
  101. <body onload='document.loginForm.username.focus();'>
  102.  
  103. <h1>Spring Security Custom Login Form (XML)</h1>
  104.  
  105. <div id="login-box">
  106.  
  107. <h3>Login with Username and Password</h3>
  108.  
  109. <c:if test="${not empty error}">
  110. <div class="error">${error}</div>
  111. </c:if>
  112. <c:if test="${not empty msg}">
  113. <div class="msg">${msg}</div>
  114. </c:if>
  115.  
  116. <form name='loginForm'
  117. action="<c:url value='/j_spring_security_check' />" method='POST'>
  118.  
  119. <table>
  120. <tr>
  121. <td>User:</td>
  122. <td><input type='text' name='username'></td>
  123. </tr>
  124. <tr>
  125. <td>Password:</td>
  126. <td><input type='password' name='password' /></td>
  127. </tr>
  128. <tr>
  129. <td colspan='2'><input name="submit" type="submit"
  130. value="submit" /></td>
  131. </tr>
  132. </table>
  133.  
  134. <input type="hidden" name="${_csrf.parameterName}"
  135. value="${_csrf.token}" />
  136.  
  137. </form>
  138. </div>
  139.  
  140. </body>
  141. </html>
  142.  
  143. <?xml version="1.0" encoding="UTF-8"?>
  144. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  145. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  146. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  147.  
  148. <context-param>
  149. <param-name>contextConfigLocation</param-name>
  150. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  151. </context-param>
  152.  
  153. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  154. <listener>
  155. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  156. </listener>
  157.  
  158. <!-- Processes application requests -->
  159. <servlet>
  160. <servlet-name>appServlet</servlet-name>
  161. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  162. <init-param>
  163. <param-name>contextConfigLocation</param-name>
  164. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  165. </init-param>
  166. <load-on-startup>1</load-on-startup>
  167. </servlet>
  168.  
  169. <servlet-mapping>
  170. <servlet-name>appServlet</servlet-name>
  171. <url-pattern>/</url-pattern>
  172. </servlet-mapping>
  173.  
  174. </web-app>
  175.  
  176. <context-param>
  177. <param-name>contextConfigLocation</param-name>
  178. <param-value>
  179. /WEB-INF/spring-security.xml
  180. </param-value>
  181. </context-param>
  182.  
  183. <!-- Spring Security -->
  184. <filter>
  185. <filter-name>springSecurityFilterChain</filter-name>
  186. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  187. </filter>
  188.  
  189. <filter-mapping>
  190. <filter-name>springSecurityFilterChain</filter-name>
  191. <url-pattern>/*</url-pattern>
  192. </filter-mapping>
  193.  
  194. <?xml version="1.0" encoding="UTF-8"?>
  195. <beans xmlns="http://www.springframework.org/schema/beans"
  196. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  197. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  198.  
  199. <!-- Root Context: defines shared resources visible to all other web components -->
  200.  
  201. </beans>
  202.  
  203. <servlet>
  204. <servlet-name>SpringController</servlet-name>
  205. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  206. <init-param>
  207. <param-name>contextConfigLocation</param-name>
  208. <param-value>/WEB-INF/spring-servlet.xml</param-value>
  209. </init-param>
  210. <load-on-startup>1</load-on-startup>
  211. </servlet>
  212.  
  213. <servlet-mapping>
  214. <servlet-name>SpringController</servlet-name>
  215. <url-pattern>/</url-pattern>
  216. </servlet-mapping>
  217.  
  218. <listener>
  219. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  220. </listener>
  221.  
  222. <context-param>
  223. <param-name>contextConfigLocation</param-name>
  224. <param-value>
  225. /WEB-INF/spring-security.xml
  226. </param-value>
  227. </context-param>
  228.  
  229. <!-- Spring Security Filter -->
  230. <filter>
  231. <filter-name>springSecurityFilterChain</filter-name>
  232. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  233. </filter>
  234.  
  235. <filter-mapping>
  236. <filter-name>springSecurityFilterChain</filter-name>
  237. <url-pattern>/*</url-pattern>
  238. </filter-mapping>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement