Shiro configuration based on spring framework

Source: Internet
Author: User

First, add Shiro filter in Web. xml

XML code
  1. <!--Shiro filter-->
  2. <filter>
  3. <filter-name>shirofilter</filter-name>
  4. <filter-class>
  5. Org.springframework.web.filter.DelegatingFilterProxy
  6. </filter-class>
  7. </filter>
  8. <filter-mapping>
  9. <filter-name>shirofilter</filter-name>
  10. <url-pattern>/*</url-pattern>
  11. </filter-mapping>


Second, add Shiro configuration in spring's Applicationcontext.xml
1. Add Shirofilter definition

XML code
  1. <!--Shiro Filter--
  2. <Bean id= "shirofilter" class="Org.apache.shiro.spring.web.ShiroFilterFactoryBean " >
  3. <property name= "securitymanager" ref="SecurityManager" />
  4. <property name= "loginurl" value="/login" />
  5. <property name= "successurl" value="/user/list" />
  6. <property name= "unauthorizedurl" value="/login" />
  7. <property name="filterchaindefinitions">
  8. <value>
  9. /login = anon
  10. /user/** = authc
  11. /role/edit/* = Perms[role:edit]
  12. /role/save = Perms[role:edit]
  13. /role/list = Perms[role:view]
  14. /** = authc
  15. </value>
  16. </Property>
  17. </Bean>


2. Add SecurityManager definition

XML code
    1. <Bean id= "securitymanager" class="Org.apache.shiro.web.mgt.DefaultWebSecurityManager " >
    2. <property name="Realm" ref="Myrealm" />
    3. </Bean>


3. Add Realm Definition

XML code
    1. <Bean id="Myrealm" class= "com ... Myrealm " />


Third, implement Myrealm: Inherit Authorizingrealm, and rewrite authentication authorization method

Java code
  1. Public class Myrealm extends authorizingrealm{
  2. private Accountmanager Accountmanager;
  3. public void Setaccountmanager (Accountmanager accountmanager) {
  4. This.accountmanager = Accountmanager;
  5. }
  6. /** 
  7. * Authorization Information
  8. */
  9. protected Authorizationinfo Dogetauthorizationinfo (
  10. PrincipalCollection principals) {
  11. String Username= (String) Principals.fromrealm (GetName ()). Iterator (). Next ();
  12. if (username! = null) {
  13. User user = Accountmanager.get (username);
  14. if (user! = null && user.getroles () = null) {
  15. Simpleauthorizationinfo info = new Simpleauthorizationinfo ();
  16. For (SecurityRole each:user.getRoles ()) {
  17. Info.addrole (Each.getname ());
  18. Info.addstringpermissions (Each.getpermissionsasstring ());
  19. }
  20. return info;
  21. }
  22. }
  23. return null;
  24. }
  25. /** 
  26. * Certification Information
  27. */
  28. protected AuthenticationInfo Dogetauthenticationinfo (
  29. Authenticationtoken Authctoken) throws authenticationexception {
  30. Usernamepasswordtoken token = (usernamepasswordtoken) Authctoken;
  31. String userName = Token.getusername ();
  32. if (userName ! = null &&! "". Equals (UserName)) {
  33. User user = Accountmanager.login (token.getusername (),
  34. String.valueof (Token.getpassword ()));
  35. if (user! = null)
  36. return new Simpleauthenticationinfo (
  37. User.getloginname (), User.getpassword (), GetName ());
  38. }
  39. return null;
  40. }
  41. }

Shiro configuration based on spring framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.