The login page: login. JSP, login success: index. The JSP,
Configuration is as follows:
XML applicationContext - shiro.
Login_ *=-anon
/JSP/register JSP=-anon
/JSP/login. JSP=-anon
/JSP/index. The JSP=authc
/toPrivilege action=roles/admin - & gt;
* *=/system/roles/admin, HR, & gt;
/* *=authc
Myrealm class implementation is as follows:
@ Component
@ Transactional
Public class MyRealm extends AuthorizingRealm {
Private LoginService LoginService;
//authorization information (doGetAuthorizationInfo)
@ Override
Protected AuthorizationInfo doGetAuthorizationInfo PrincipalCollection (principals)
{
System. Out.println (" authorization information... ");
//derives from the principals on the user input the login name
String loginname=(String) principals. FromRealm (getName ()). The iterator (). The next ();//use the iterator
//from the database to determine whether a user is
User User=loginService. FindUserByName (loginname);
If (the user!=null)
{
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo ();
Set
For (Role Role: roles)
{
//add the name of the character to the authorization information
Info. AddRole (role. GetRole_name ());
//get the role of corresponding permissions
Set
For (Permission Permission: permissions)
{
Info. AddStringPermission (permission. GetValidateurl ());
}
}
return info;
}
return null;
}
//get the authentication information (doGetAuthenticationInfo
@ Override
Protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken token) throws AuthenticationException
{
System. Out.println (" authentication information... ");
//conversion become easier the UsernamePasswordToken object
The UsernamePasswordToken usertoken=(the UsernamePasswordToken) token;
User User=loginService. FindUserByName (usertoken getUsername ());
If (the user!=null)
{
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo (user getUsername (), usertoken. GetPassword (), getName ());
return info;
}
return null;
}
Public LoginService getLoginService () {
Return loginService;
}
Public void setLoginService (LoginService LoginService) {
Enclosing loginService=loginService;
}
}
The login. The JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<meta HTTP - equiv="content-type" Content="text/HTML. Charset=utf-8 "& gt;
<body>