Shiroç®ä»
Shiroæ¯Apacheä¸çä¸ä¸ªç¨äºèº«ä»½æ ¡éªï¼ææï¼ä¼è¯ç®¡ççä¸ä¸ªå®å ¨æ¡æ¶ãå®è½å¤åºç¨å¨ä»»ä½éè¦å®å ¨æ ¡éªçåºåã
å®ä¸»è¦ç±ä¸ä¸ªæ ¸å¿ç»ä»¶ææï¼
- Subjectï¼ä¸»ä½ï¼å³âå½åç¨æ·âã主ä½ä¸ä» å¯ä»¥æ¯ç³»ç»ç¨æ·ï¼ä¹æå¯è½æ¯ç¬¬ä¸æ¹è¿ç¨ï¼å ¶ä»åºç¨ç¨åºççã
- SecurityManagerï¼å®æ¯Shiroæ¡æ¶çæ ¸å¿ãå®éè¿é¨é¢æ¨¡å¼è¿è¡æé ï¼å°SubjectåRealmèåå°å ¶å é¨ï¼ä»¥æ¤æä¾åç§å®å ¨ç®¡ççæå¡ã
- Realmï¼åï¼ç¨äºé ç½®ç¸å ³ç¨æ·åå ¶æéä¿¡æ¯ãå¨è¿è¡ç»é认è¯ä»¥åææ访é®æ¶ï¼è½å¤åSecurityManageræä¾ææä¿¡æ¯ãåæ¶ï¼ç¨æ·å¯ä»¥èªå®ä¹Realmç认è¯æºå¶ï¼æ¥è¾¾å°æééªè¯çå¤æåã
Shiroæ¶æ
shiroæ¶æå¾
ä»»ä½ç主ä½ï¼æ 论æ¯c++ç¨åºè¿æ¯Javaç¨åºï¼ï¼å¨æææ¶é½ä¼ç»ç±SecuirityManagerå¤çãå¦ææ¯ææé®é¢ï¼åç±Authorizerå¤çï¼å¦ææ¯è®¤è¯ï¼åç±Authenticatorå¤çãæææçæ¯å°åºç¨çæåèµäºå ¶ä»ä¸»ä½ï¼å ¶ä¸ä¸»ä½æ¯å·²ç»ç»è¿è®¤è¯ç主ä½ãè认è¯æ¯å°ä¸ºç»è¿è®¤è¯ç主ä½è¿è¡è®¤è¯ï¼å°å ¶å¤å®ä¸ºç³»ç»è§è²æéç³»ç»è§è²ã
åæ¶ï¼Security Managerä¹éæäºRealmsï¼åï¼å¯ä»¥è®¤ä¸ºæ¯ç¨æ·èº«ä»½ä¿¡æ¯çéåï¼ï¼ä¼è¯ç®¡çï¼Session Managerï¼åç¼å管çã
Shiroæ¶æçå ·ä½å®ç°
é¦å æ¯Subject主ä½ã
å®æ¯ä¸ä¸ªå®ä¹å¥½çæ¥å£ï¼å®ä¹äºä¸ä¸ªä¸»ä½å¨ææ认è¯ä½ç³»ä¸å¯è½åºç°çä¸äºå¨ä½ï¼
å ¶ä¸å®ä¹äºä¸äºå¸¸è§çæ¹æ³ï¼
- isPermittedæ¯å¦è¢«è®¤è¯ï¼åæ°å¯ä»¥æ¯Permissionï¼æè 继æ¿è¯¥ç±»ï¼ï¼Stringï¼å¦"é 读ææææ¡£æé","æ¥çå工信æ¯æé"çï¼ï¼ä»¥åStringæ°ç»ã
- checkPermissionãç¸å ³åæ°åä¸
- hasRoleï¼checkRoleçè§è²ç¸å ³å½æ°ï¼
- loginålogoutç»ééåºå¨ä½
- ä¸äºæ¯æå½æ°çã
å ¶æ¬¡æ¯Realmï¼
ä¹æ¯ä¸ªæ¥å£å®ä¹ï¼å®ä¹äºsupportsï¼ç¨äºæ¯æAuthenticationTokenççæï¼åæ¶ä¹å®ä¹äºgetAuthenticationInfoï¼éè¿tokenè·å¾AuthenticationInfoï¼è®¤è¯ä¿¡æ¯ï¼ï¼æ交ç»ä¸çº§ï¼SecurityManager)ä¸çAuthzï¼Authorizerç®ç§°ï¼åAuthcï¼Authenticatorï¼è¿è¡è®¤è¯åé´æã
æåæ¯SecurityManagerï¼
å®æ¯ä¸ä¸ªå¾ç®åçæ¥å£ï¼å®ä¹äºä¸äºåºç¡çæ¹æ³ï¼æºä»£ç å¦ä¸ï¼
public interface SecurityManager extends Authenticator, Authorizer, SessionManager {
Subject login(Subject var1, AuthenticationToken var2) throws AuthenticationException;
void logout(Subject var1);
Subject createSubject(SubjectContext var1);
}
å¯ä»¥åç°ï¼ä»ç»§æ¿äºAuthenticatorï¼Authorizerï¼SessionManagerï¼å¹¶å°Subjectçloginï¼logoutå¨ä½éåå¨å ¶ä¸ï¼æ以ï¼è¿äºä»£ç å®ä¹äºShiroçæ´ä¸ªæ¶æã
å´ç»ä¸ä¸ªç»ä»¶çå ¶ä»ç»ä»¶
对äºSubject主ä½èè¨ï¼ä¸äºè¯¸å¦Permissionï¼ä¸»ä½ææ¥æçæéï¼ï¼Roleï¼ä¸»ä½çè§è²ï¼çç±»ï¼é½æ¯ä¸äºåååçç设置ã
对äºRealmèè¨ï¼åæ¯AuthenticationInfoï¼ä¸ä¸ªç¨æ·ç»è¿è®¤è¯åçæç认è¯ä¿¡æ¯ï¼ä»¥åAuthenticationTokenï¼ä¸ä¸ªç¨æ·æææçåè¯ï¼é常æ¯ç»éæ¶ï¼æ ¡éªç¨æ·å¯ç ï¼éè¿åé¢åï¼
对äºSecurityManagerï¼Authenticatorä¸åªå®ä¹äºauthenticateæ¹æ³ï¼å¹¶æä¸ç³»åçé»è®¤å®ç°ãèAuthorizerä¸å®ä¹äºå¾å¤æéç¸å ³çæ¹æ³ãèSessionManageræ¯Shiroçæ¡æ¶æ¯æï¼å®æä¾äºä¸äºå®å ¨ä¼è¯çåè½ã
ç¹å«è¯´æ
Shiroæ¡æ¶æ¯ä¸ä¸ªæ¯è¾ç®åçå®å ¨æ¡æ¶ï¼å®å°æ们æ¥å¸¸ç¼ç ä¸çææ认è¯æ½è±¡æä¸å¥ä½ç³»ï¼ä¸»è¦å´ç»ä¸ä¸ªæ ¸å¿ç»ä»¶æ¥è¿è¡æ¶æã读è å¯ä»¥å é¢ä¼Shiroæ¡æ¶çæ¶æææ³ï¼åé åä¸é¢çä¾åè¿è¡æ·±å»çç解ãè³äºShiroä¸çæ°æ®ç»æï¼è¯¸å¦Permissionä¹ç±»çï¼è¯»è åªé大è´äºè§£å ¶å«ä¹ååè½ï¼æ é太è¿æ§çäºå®ä»¬çå ·ä½å®ä¹ã
Shiroæ¡æ¶å®è·µæ¡ä¾
æäºç论ç¥è¯åï¼å°±å¼å§å®è·µæ建ä¸å¥æ¡ä¾æ¥è¿è¡å®é è¿ç¨ã
ææ¯è¯´æï¼
æ¬æ¡ä¾çæ建åºäºJava 8ç¯å¢ï¼ä½¿ç¨çæ¯SpringBootæ¡æ¶ï¼éç¨Mavenè¿è¡é¡¹ç®æ建ã使ç¨çç¸å ³å¤é¨æ¡æ¶ççæ¬å¦ä¸ï¼
- Shiroçæ¬ä¸º1.4.0çæ¬
- MyBatis-plus-starterçæ¬ä¸º1.0.5
- MyBatis-plusçæ¬ä¸º2.1.9
项ç®ç»æ
âShiroDemoâ
â --srcâ
â --mainâ
â --java //项ç®æ件ç®å½â
â --com.jungle //主ç®å½â
â --config //项ç®ç¸å ³é ç½®â
â --controller //æ§å¶å±â
â --domain //å®ä½ç±»â
â --mapper //dao â
â --service //æå¡å±â
â --resource //èµæºæ件ç®å½â
â --mapper //mapperæ å°æ件æå¨å¤â
â --test //项ç®æµè¯æ件â
â --pom.xml //maven项ç®æ件â
å¼å§ ---è¿è¡ç®åçæ°æ®åºæ建
å ¶å®ï¼å½ä½ å¼å ¥Shiroçå æ¶ï¼è¿ä¸ªå®å ¨çæºå¶å°±å·²ç»å»ºç«æäºãä½æ¯å¢ï¼Shiroåªæ¯ä¸ä¸ªæ¡æ¶ï¼åªæ¯æä¾ä¸ä¸ªåºç¡ï¼æ们éè¦æ ¹æ®éæ±æ¥å®å¶åæ们çå®å ¨æºå¶ã
é£ä¹ï¼æ们æ¥æ¨¡æä¸ä¸ªæ¯è¾å¸¸è§çåºæ¯ï¼
é¦å ï¼ä¸ä¸ªç³»ç»æ»æ¯æç¨æ·ï¼é¤äºç¨æ·ä»¥å¤ï¼è¿æè§è²ï¼æ¯å¦ç³»ç»ç®¡çåï¼æ®éç¨æ·ï¼ï¼æéï¼é 读ææææ¡£çæéï¼ã为äºå°è¿äºè§è²å ³èèµ·æ¥ï¼éè¦å»ºç«å ³è表ï¼å ¶ä¸ç¨æ·äºè§è²æ¯ä¸å¯¹ä¸çå ³ç³»ï¼è§è²åæéæ¯ä¸å¯¹å¤çå ³ç³»ã
E-Rå¾å¤§è´å¦ä¸ï¼
ç¸å ³å»ºè¡¨è¯å¥å¦ä¸ï¼
ç¨æ·è¡¨ï¼
CREATE TABLE `user_inf` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`password` varchar(128) NOT NULL,
`salt` varchar(12) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=10004 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
è§è²è¡¨ï¼
CREATE TABLE `role_inf` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=107 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ç¨æ·-è§è²å ³è表ï¼
CREATE TABLE `user_role_inf` (
`user_id` int(11) NOT NULL,
`role_id` int(11) NOT NULL,
KEY `user_foreign_constraint` (`user_id`),
KEY `role2_foreign_constraint` (`role_id`),
CONSTRAINT `role2_foreign_constraint` FOREIGN KEY (`role_id`) REFERENCES `role_inf` (`id`),
CONSTRAINT `user_foreign_constraint` FOREIGN KEY (`user_id`) REFERENCES `user_inf` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
æé表ï¼
CREATE TABLE `permission_inf` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`permission_name` varchar(20) NOT NULL,
`permission_url` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
è§è²-æéå ³è表ï¼
CREATE TABLE `role_permission_inf` (
`role_id` int(11) NOT NULL,
`permission_id` int(11) NOT NULL,
KEY `role_foreign_constraint` (`role_id`),
KEY `permission_foreign_constraint` (`permission_id`),
CONSTRAINT `permission_foreign_constraint` FOREIGN KEY (`permission_id`) REFERENCES `permission_inf` (`id`),
CONSTRAINT `role_foreign_constraint` FOREIGN KEY (`role_id`) REFERENCES `role_inf` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ç»§ç» ---è¿è¡ç®åçé ç½®
æShiroå¼å ¥è¿æ¥åï¼å°±å¯ä»¥è¿è¡ä¸äºé ç½®äºã
é¦å ï¼æ们éè¦é ç½®Shiroçæ ¸å¿ï¼é ç½®SecurityManagerï¼ç¸å ³ä»£ç å¦ä¸ï¼
@Bean
è¿éï¼securityManager使ç¨Realmæ¯èªå®ä¹çï¼ä¹æ¯éè¦æ们èªå·±å»é ç½®çï¼å®çé ç½®å¦ä¸ï¼
@Bean
MyShiroRealmï¼
public
è¿éæ们继æ¿AuthorizingRealmç±»ï¼è¯¥ç±»æ¯ä¸ä¸ªæ½è±¡ç±»ï¼å¯¹äºå¸¸è§çææå认è¯é½æäºè¾å¥½çå®ç°ï¼åªè¦å»å®ç°å®çdoGetAuthorizationInfoæ¹æ³å³å¯ã注æï¼è¯¥æ¹æ³è¢«éè½½æäºä¸¤ä¸ªæ¹æ³ï¼å ¶ä¸ä¸ä¸ªæ¥æ¶çæ¯PrincipalCollectionï¼å¦ä¸ä¸ªæ¥æ¶çæ¯AuthenticationTokenã
第ä¸ä¸ªæ¥æ¶PrincipalCollectionçæ¯æä¹å®ç°çå¢ï¼é¦å ï¼ä»»ä½ç¨æ·ç»éé½ä¼ç»è¿Shiroï¼ä½æ¯æ们ç¨æ·ç»éè¿å»ï¼é½ä¼è¢«å è£ æPrincipalCollectionãç¶ååè¿å ¥å°Realmä¸ï¼æ们è¦å°èªå·±çæéè§ååè¯Shiroæ¡æ¶ï¼æ以就éè¦ç»ä¸å è£ æPrincipalCollectionï¼ç¶å使ç¨getPrimaryPrincipalæ¥è·åå½åçç¨æ·ãç¶ååç»è¿ä¸äºæ¥è¯¢æä½ï¼å°è§è²æéå ³ç³»éè¿ç¼ç æè½½Shiroæ¡æ¶ä¹ä¸ãæ¥çï¼åªè¦æç §è§åå è£ å¥½AuthorizationInfoå³å¯ã
第äºä¸ªæ¥æ¶AuthenticationTokenï¼è¿ä¸ªtoken主è¦å æ¬çæ¯Principalï¼å¼ºå¶è½¬æ¢åå°±æ¯å¯¹åºçSubjectäºï¼ååè¯ãç¶åè¿ä¸ªæ¹æ³å°±æ¯ç¨äºè¿è¡ç»ééªè¯çã
é£ä¹é®é¢å°±æ¥äºï¼å¦æç»é失败ï¼é£ä¹è¦æä¹å¤çè¿äºé误信æ¯å¢ï¼Shiroæä¾äºä¸å¥çFilteræºå¶ï¼ç¨æ¥è¿æ»¤ç¸å ³ä¿¡æ¯çãå®ç°å¦ä¸ï¼
public
è¿éæ们åªéåäºsetFailureAttributeæ¹æ³ãå ¶å®ï¼ç»§æ¿çè¿ä¸ªFormAuthenticationFilteræå¾æ·±çâååºâï¼æ们ä¸ååæã
ç±äºæ¬äººé¦æ¬¡å¨ç¥ä¹åä½ï¼æ以å¦æä¸è¶³è¿è¯·è§è° ãç±äºè²ä¼¼ç¥ä¹åç¯æç« æåæ°éå¶è¿æ¯å ¶ä»ä»ä¹åå ï¼æ å为两è书åã
è¿ç¯æç« ç»å¤§é¨åå 容为èªåå 容ï¼ä¸è¿æé¨åå 容å¦Realmï¼MyAuthenticationFilterçç¸å ³å 容æ¯åé´Spring Boot [éæ-Shiro] çæç« ï¼å¹¶å®å°éªè¯äºä¸éï¼æ²¡æé®é¢ï¼è½¬è½½è¯·è¯´æåºå¤ï¼ååæè°¢ã