Guest User

Untitled

a guest
May 3rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 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/security
  6. http://www.springframework.org/schema/security/spring-security-4.2.xsd
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  9.  
  10. <!--<global-method-security secured-annotations="enabled" />-->
  11. <http auto-config="true" use-expressions="true" >
  12. <intercept-url pattern="/mylogin" access="permitAll"/>
  13. <intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" />
  14. <form-login
  15. login-page="/mylogin"
  16. default-target-url="/admin"
  17. authentication-failure-url="/accessDenied"
  18. username-parameter="txt_user"
  19. password-parameter="txt_pwd"/>
  20. <csrf disabled="true"/>
  21. </http>
  22.  
  23. <authentication-manager>
  24. <authentication-provider>
  25. <user-service>
  26. <user name="admin" password="123" authorities="ROLE_ADMIN"/>
  27. </user-service>
  28. </authentication-provider>
  29. </authentication-manager>
  30.  
  31. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  32. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  33. <!DOCTYPE html>
  34. <html>
  35. <head>
  36. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  37. <title>JSP Page</title>
  38. </head>
  39. <body>
  40. <h1>Hello World!</h1>
  41. <form name="frmLogin" method="POST" action="<c:url value='mylogin' />">
  42. <table>
  43. <tr>
  44. <td>Enter username: </td>
  45. <td><input type="text" name="txt_user" /></td>
  46. </tr>
  47. <tr>
  48. <td>Enter password: </td>
  49. <td><input type="password" name="txt_pwd" /></td>
  50. </tr>
  51. <tr>
  52. <td colspan="2">
  53. <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
  54. <input type="submit" name="btn_sub" value="OK"/>
  55. </td>
  56. </tr>
  57. </table>
  58. </form>
  59. </body>
  60.  
  61. @RequestMapping(value="/admin", method=RequestMethod.GET)
  62. public String adminPage(Model model, Principal principal)
  63. {
  64. model.addAttribute("user", principal.getName());
  65. return "admin";
  66. }
  67.  
  68. @RequestMapping("/mylogin")
  69. public String loginPage(Model model)
  70. {
  71. return "customLogin";
  72. }
  73.  
  74. @RequestMapping(value="/logout", method = RequestMethod.GET)
  75. public String logoutPage (HttpServletRequest request, HttpServletResponse response)
  76. {
  77. Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  78. if(auth != null)
  79. new SecurityContextLogoutHandler().logout(request, response, auth);
  80.  
  81. return "redirect:/login?logout";
  82. }
  83.  
  84. @RequestMapping(value="/accessDenied", method = RequestMethod.GET)
  85. public String accessDeniedPage(Model model)
  86. {
  87. model.addAttribute("user", getPrincipal());
  88. return "accessDenied";
  89. }
Add Comment
Please, Sign In to add comment