1.åç¹ç»å½SSOåçåå®ç°
2.è°è½ä»ç»ä¸äºåç¹ç»å½ç³»ç»
åç¹ç»å½SSOåçåå®ç°
欢è¿å¤§å®¶å ³æ³¨?单点登单点登录仙剑online 源码github.com/hsfxuebao?ï¼å¸æ对大家ææ帮å©ï¼è¦æ¯è§å¾å¯ä»¥çè¯éº»ç¦ç»ç¹ä¸ä¸Starå1. åç³»ç»ç»å½æºå¶1.1 / å?/index.html ?å¦ åæºï¼åè®®ãååã端å£å·ç¸åï¼/ å?/index.html?è·¨å åè®®ä¸åï¼/ å?ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é?è·¨å 主ååä¸åï¼test/baiduï¼
/? å /?è·¨å åååä¸åï¼www/blogï¼
:/?å :/?è·¨å 端å£å·ä¸åï¼/ï¼
2.3 å¤ç³»ç»è§£å³æ¹æ¡è½ç¶åç³»ç»çç»å½è§£å³æ¹æ¡å¾å®ç¾ï¼ä½å¯¹äºå¤ç³»ç»åºç¨ç¾¤å·²ç»ä¸åéç¨äºï¼ä¸ºä»ä¹å¢ï¼
åç³»ç»ç»å½è§£å³æ¹æ¡çæ ¸å¿æ¯cookieï¼cookieæºå¸¦ä¼è¯idå¨æµè§å¨ä¸æå¡å¨ä¹é´ç»´æ¤ä¼è¯ç¶æãä½cookieæ¯æéå¶çï¼è¿ä¸ªéå¶å°±æ¯cookieçåï¼é常对åºç½ç«çååï¼ï¼æµè§å¨åéâï¼ç¶åå°å®ä»¬çcookieå设置为âbaidu.comâï¼è¿ç§åæ³ç论ä¸æ¯å¯ä»¥çï¼çè³æ©æå¾å¤å¤ç³»ç»ç»å½å°±éç¨è¿ç§åååå ±äº«cookieçæ¹å¼ãç¶èï¼å¯è¡å¹¶ä¸ä»£è¡¨å¥½ï¼å ±äº«cookieçæ¹å¼åå¨ä¼å¤å±é:
é¦å ï¼åºç¨ç¾¤ååå¾ç»ä¸ï¼
å ¶æ¬¡ï¼åºç¨ç¾¤åç³»ç»ä½¿ç¨çææ¯ï¼è³å°æ¯webæå¡å¨ï¼è¦ç¸åï¼ä¸ç¶cookieçkeyå¼ï¼tomcat为JSESSIONIDï¼ä¸åï¼æ æ³ç»´æä¼è¯ï¼å ±äº«cookieçæ¹å¼æ¯æ æ³å®ç°è·¨è¯è¨ææ¯å¹³å°ç»å½çï¼æ¯å¦javaãphpã.netç³»ç»ä¹é´ï¼
第ä¸ï¼cookieæ¬èº«ä¸å®å ¨ã
å æ¤ï¼æ们éè¦ä¸ç§å ¨æ°çç»å½æ¹å¼æ¥å®ç°å¤ç³»ç»åºç¨ç¾¤çç»å½ï¼è¿å°±æ¯åç¹ç»å½
3. åç¹ç»å½ä»ä¹æ¯åç¹ç»å½ï¼åç¹ç»å½å ¨ç§°Single Sign Onï¼ä»¥ä¸ç®ç§°SSOï¼ï¼æ¯æå¨å¤ç³»ç»åºç¨ç¾¤ä¸ç»å½ä¸ä¸ªç³»ç»ï¼ä¾¿å¯å¨å ¶ä»ææç³»ç»ä¸å¾å°ææèæ éå次ç»å½ï¼å æ¬åç¹ç»å½ä¸åç¹æ³¨é两é¨å
3.1 ç»å½ç¸æ¯äºåç³»ç»ç»å½ï¼ssoéè¦ä¸ä¸ªç¬ç«ç认è¯ä¸å¿ï¼åªæ认è¯ä¸å¿è½æ¥åç¨æ·çç¨æ·åå¯ç çå®å ¨ä¿¡æ¯ï¼å ¶ä»ç³»ç»ä¸æä¾ç»å½å ¥å£ï¼åªæ¥å认è¯ä¸å¿çé´æ¥ææãé´æ¥ææéè¿ä»¤çå®ç°ï¼sso认è¯ä¸å¿éªè¯ç¨æ·çç¨æ·åå¯ç 没é®é¢ï¼å建ææ令çï¼å¨æ¥ä¸æ¥ç跳转è¿ç¨ä¸ï¼ææ令çä½ä¸ºåæ°åéç»å个åç³»ç»ï¼åç³»ç»æ¿å°ä»¤çï¼å³å¾å°äºææï¼å¯ä»¥åæ¤å建å±é¨ä¼è¯ï¼å±é¨ä¼è¯ç»å½æ¹å¼ä¸åç³»ç»çç»å½æ¹å¼ç¸åãè¿ä¸ªè¿ç¨ï¼ä¹å°±æ¯åç¹ç»å½çåçï¼ç¨ä¸å¾è¯´æ
ä¸é¢å¯¹ä¸å¾ç®è¦æè¿°:
ç¨æ·è®¿é®ç³»ç»1çåä¿æ¤èµæºï¼ç³»ç»1åç°ç¨æ·æªç»å½ï¼è·³è½¬è³sso认è¯ä¸å¿ï¼å¹¶å°èªå·±çå°åä½ä¸ºåæ°
sso认è¯ä¸å¿åç°ç¨æ·æªç»å½ï¼å°ç¨æ·å¼å¯¼è³ç»å½é¡µé¢
ç¨æ·è¾å ¥ç¨æ·åå¯ç æ交ç»å½ç³è¯·
sso认è¯ä¸å¿æ ¡éªç¨æ·ä¿¡æ¯ï¼å建ç¨æ·ä¸sso认è¯ä¸å¿ä¹é´çä¼è¯ï¼ç§°ä¸ºå ¨å±ä¼è¯ï¼åæ¶å建ææ令ç
sso认è¯ä¸å¿å¸¦ç令ç跳转ä¼æåç请æ±å°åï¼ç³»ç»1ï¼
ç³»ç»1æ¿å°ä»¤çï¼å»sso认è¯ä¸å¿æ ¡éªä»¤çæ¯å¦ææ
sso认è¯ä¸å¿æ ¡éªä»¤çï¼è¿åææï¼æ³¨åç³»ç»1
ç³»ç»1使ç¨è¯¥ä»¤çå建ä¸ç¨æ·çä¼è¯ï¼ç§°ä¸ºå±é¨ä¼è¯ï¼è¿ååä¿æ¤èµæº
ç¨æ·è®¿é®ç³»ç»2çåä¿æ¤èµæº
ç³»ç»2åç°ç¨æ·æªç»å½ï¼è·³è½¬è³sso认è¯ä¸å¿ï¼å¹¶å°èªå·±çå°åä½ä¸ºåæ°
sso认è¯ä¸å¿åç°ç¨æ·å·²ç»å½ï¼è·³è½¬åç³»ç»2çå°åï¼å¹¶éä¸ä»¤ç
ç³»ç»2æ¿å°ä»¤çï¼å»sso认è¯ä¸å¿æ ¡éªä»¤çæ¯å¦ææ
sso认è¯ä¸å¿æ ¡éªä»¤çï¼è¿åææï¼æ³¨åç³»ç»2
ç³»ç»2使ç¨è¯¥ä»¤çå建ä¸ç¨æ·çå±é¨ä¼è¯ï¼è¿ååä¿æ¤èµæº
ç¨æ·ç»å½æåä¹åï¼ä¼ä¸sso认è¯ä¸å¿åå个åç³»ç»å»ºç«ä¼è¯ï¼ç¨æ·ä¸sso认è¯ä¸å¿å»ºç«çä¼è¯ç§°ä¸ºå ¨å±ä¼è¯ï¼ç¨æ·ä¸å个åç³»ç»å»ºç«çä¼è¯ç§°ä¸ºå±é¨ä¼è¯ï¼å±é¨ä¼è¯å»ºç«ä¹åï¼ç¨æ·è®¿é®åç³»ç»åä¿æ¤èµæºå°ä¸åéè¿sso认è¯ä¸å¿ï¼å ¨å±ä¼è¯ä¸å±é¨ä¼è¯æå¦ä¸çº¦æå ³ç³»:
å±é¨ä¼è¯åå¨ï¼å ¨å±ä¼è¯ä¸å®åå¨
å ¨å±ä¼è¯åå¨ï¼å±é¨ä¼è¯ä¸ä¸å®åå¨
å ¨å±ä¼è¯éæ¯ï¼å±é¨ä¼è¯å¿ é¡»éæ¯
ä½ å¯ä»¥éè¿å客åãç¾åº¦ãcsdnãæ·å®çç½ç«çç»å½è¿ç¨å 深对åç¹ç»å½çç解ï¼æ³¨æè§å¯ç»å½è¿ç¨ä¸ç跳转urlä¸åæ°
3.2 注éåç¹ç»å½èªç¶ä¹è¦åç¹æ³¨éï¼å¨ä¸ä¸ªåç³»ç»ä¸æ³¨éï¼ææåç³»ç»çä¼è¯é½å°è¢«éæ¯ï¼ç¨ä¸é¢çå¾æ¥è¯´æ
sso认è¯ä¸å¿ä¸ç´çå¬å ¨å±ä¼è¯çç¶æï¼ä¸æ¦å ¨å±ä¼è¯éæ¯ï¼çå¬å¨å°éç¥ææ注åç³»ç»æ§è¡æ³¨éæä½
ä¸é¢å¯¹ä¸å¾ç®è¦è¯´æ:
ç¨æ·åç³»ç»1å起注é请æ±
ç³»ç»1æ ¹æ®ç¨æ·ä¸ç³»ç»1建ç«çä¼è¯idæ¿å°ä»¤çï¼åsso认è¯ä¸å¿å起注é请æ±
sso认è¯ä¸å¿æ ¡éªä»¤çææï¼éæ¯å ¨å±ä¼è¯ï¼åæ¶ååºææç¨æ¤ä»¤ç注åçç³»ç»å°å
sso认è¯ä¸å¿åææ注åç³»ç»å起注é请æ±
å注åç³»ç»æ¥æ¶sso认è¯ä¸å¿ç注é请æ±ï¼éæ¯å±é¨ä¼è¯
sso认è¯ä¸å¿å¼å¯¼ç¨æ·è³ç»å½é¡µé¢
4. å®ç°åªæ¯ç®è¦ä»ç»ä¸åºäºjavaçå®ç°è¿ç¨ï¼ä¸æä¾å®æ´æºç ï¼æç½äºåçï¼æç¸ä¿¡ä½ 们å¯ä»¥èªå·±å®ç°ãssoéç¨å®¢æ·ç«¯/æå¡ç«¯æ¶æï¼æ们å çsso-clientä¸sso-serverè¦å®ç°çåè½ï¼ä¸é¢ï¼sso认è¯ä¸å¿=sso-serverï¼
sso-client:
æ¦æªåç³»ç»æªç»å½ç¨æ·è¯·æ±ï¼è·³è½¬è³sso认è¯ä¸å¿
æ¥æ¶å¹¶åå¨sso认è¯ä¸å¿åéç令ç
ä¸sso-serveréä¿¡ï¼æ ¡éªä»¤ççæææ§
建ç«å±é¨ä¼è¯
æ¦æªç¨æ·æ³¨é请æ±ï¼åsso认è¯ä¸å¿åé注é请æ±
æ¥æ¶sso认è¯ä¸å¿ååºç注é请æ±ï¼éæ¯å±é¨ä¼è¯
sso-server:
éªè¯ç¨æ·çç»å½ä¿¡æ¯
åå»ºå ¨å±ä¼è¯
å建ææ令ç
ä¸sso-clientéä¿¡åé令ç
æ ¡éªsso-client令çæææ§
ç³»ç»æ³¨å
æ¥æ¶sso-client注é请æ±ï¼æ³¨éææä¼è¯
æ¥ä¸æ¥ï¼æ们æç §åçæ¥ä¸æ¥æ¥å®ç°ssoå§ï¼
4.1 sso-clientæ¦æªæªç»å½è¯·æ±javaæ¦æªè¯·æ±çæ¹å¼æservletãfilterãlistenerä¸ç§æ¹å¼ï¼æ们éç¨filterãå¨sso-clientä¸æ°å»ºLoginFilter.java类并å®ç°Filteræ¥å£ï¼å¨doFilter()æ¹æ³ä¸å å ¥å¯¹æªç»å½ç¨æ·çæ¦æª
public?void?doFilter(ServletRequest?request,?ServletResponse?response,?FilterChain?chain)?throws?IOException,?ServletException?{ HttpServletRequest?req?=?(HttpServletRequest)?request;HttpServletResponse?res?=?(HttpServletResponse)?response;HttpSession?session?=?req.getSession();if?(session.getAttribute("isLogin"))?{ chain.doFilter(request,?response);return;}//跳转è³sso认è¯ä¸å¿res.sendRedirect("sso-server-url-with-system-url");}4.2 sso-serveræ¦æªæªç»å½è¯·æ±æ¦æªä»sso-client跳转è³sso认è¯ä¸å¿çæªç»å½è¯·æ±ï¼è·³è½¬è³ç»å½é¡µé¢ï¼è¿ä¸ªè¿ç¨ä¸sso-clientå®å ¨ä¸æ ·
4.3 sso-serveréªè¯ç¨æ·ç»å½ä¿¡æ¯ç¨æ·å¨ç»å½é¡µé¢è¾å ¥ç¨æ·åå¯ç ï¼è¯·æ±ç»å½ï¼sso认è¯ä¸å¿æ ¡éªç¨æ·ä¿¡æ¯ï¼æ ¡éªæåï¼å°ä¼è¯ç¶ææ 记为âå·²ç»å½â
@RequestMapping("/login")public?String?login(String?username,?String?password,?HttpServletRequest?req)?{ this.checkLoginInfo(username,?password);req.getSession().setAttribute("isLogin",?true);return?"success";}4.4ãsso-serverå建ææ令çææ令çæ¯ä¸ä¸²éæºå符ï¼ä»¥ä»ä¹æ ·çæ¹å¼çæé½æ²¡æå ³ç³»ï¼åªè¦ä¸éå¤ãä¸æ伪é å³å¯ï¼ä¸é¢æ¯ä¸ä¸ªä¾å
String?token?=?UUID.randomUUID().toString();4.5ãsso-clientåå¾ä»¤çå¹¶æ ¡éªsso认è¯ä¸å¿ç»å½åï¼è·³è½¬ååç³»ç»å¹¶éä¸ä»¤çï¼åç³»ç»ï¼sso-clientï¼åå¾ä»¤çï¼ç¶åå»sso认è¯ä¸å¿æ ¡éªï¼å¨LoginFilter.javaçdoFilter()ä¸æ·»å å è¡
//?请æ±é带tokenåæ°String?token?=?req.getParameter("token");if?(token?!=?null)?{ //?å»sso认è¯ä¸å¿æ ¡éªtokenboolean?verifyResult?=?this.verify("sso-server-verify-url",?token);if?(!verifyResult)?{ res.sendRedirect("sso-server-url");return;}chain.doFilter(request,?response);}verify()æ¹æ³ä½¿ç¨/xuxueli/xxlâ¦Downloadgitee.com/xuxueliâ¦Download5.4 ææ¡£å°å
ä¸æææ¡£
5.5 项ç®ç»æ说æHttpSession?session?=?request.getSession();session.getAttribute("isLogin");.6 åºäºTokenæ¹å¼é¨ç½²ç±äºåå端å离å¼åç模å¼è¾å¤ï¼è¿éåªä»ç»åºäºTokenæ¹å¼é¨ç½²ï¼å¨ä¸äºæ æ³ä½¿ç¨Cookieçåºæ¯ä¸ï¼å¯ä½¿ç¨è¯¥æ¹å¼ï¼å¦éè¦Cookieæ¥çåºäºcookieæ¹å¼é¨ç½²
5.6.1 认è¯ä¸å¿ï¼SSO Serverï¼æ建项ç®åï¼xxl-sso-server
é ç½®æ件ä½ç½®ï¼application.properties
HttpSession?session?=?request.getSession();session.getAttribute("isLogin");.6.2 åç¹ç»éClient端æ建项ç®åï¼xxl-sso-token-sample-springboot
mavenä¾èµ
HttpSession?session?=?request.getSession();session.getAttribute("isLogin");3é ç½®æ件ï¼application.properties
HttpSession?session?=?request.getSession();session.getAttribute("isLogin");4é ç½® XxlSsoTokenFilter
HttpSession?session?=?request.getSession();session.getAttribute("isLogin");.6.3 éªè¯ (模æè¯·æ± Token æ¹å¼æ¥å ¥SSOçæ¥å£)ä¿®æ¹Hostæ件ï¼ååæ¹å¼è®¿é®è®¤è¯ä¸å¿ï¼æ¨¡æè·¨åä¸çº¿ä¸çå®ç¯å¢
HttpSession?session?=?request.getSession();session.getAttribute("isLogin");6åå«è¿è¡ "xxl-sso-server" ä¸ "xxl-sso-token-sample-springboot"
认è¯ä¸å¿æ建æååï¼é»è®¤ä¸ºTokenæ¹å¼ç»éæä¾APIæ¥å£:
1ãç»éæ¥å£ï¼/app/login
codeï¼ è¡¨ç¤ºæåãå ¶ä»å¤±è´¥
msgï¼é误æ示
data: ç»éç¨æ·ç sso sessionid
usernameï¼è´¦å·
passwordï¼è´¦å·
åæ°ï¼POSTåæ°
ååºï¼JSONæ ¼å¼
2ã注éæ¥å£ï¼/app/logout
codeï¼ è¡¨ç¤ºæåãå ¶ä»å¤±è´¥
msgï¼é误æ示
sessionIdï¼ç»éç¨æ·ç sso sessionid
åæ°ï¼POSTåæ°
ååºï¼JSONæ ¼å¼
3ãç»éç¶ææ ¡éªæ¥å£ï¼/app/l
è°è½ä»ç»ä¸äºåç¹ç»å½ç³»ç»
tomcatççä¸è½½å°å
è¯è ï¼éå
--
ç¼è æï¼ç°å¨å¼åJava Webåºç¨ï¼å»ºç«åé¨ç½²Webå 容æ¯ä¸ä»¶å¾ç®åçå·¥ä½ã使ç¨Jakarta Tomcatä½ä¸ºServletåJSP容å¨ç人已ç»éåå ¨ä¸çãTomcatå ·æå è´¹ã跨平å°ç诸å¤ç¹æ§ï¼å¹¶ä¸æ´æ°å¾å¾å¿«ï¼ç°å¨é常çæµè¡ã
ä½ æéè¦åçå°±æ¯ï¼æç §ä½ çéæ±é ç½®Tomcatï¼åªè¦ä½ æ£ç¡®é ç½®ï¼Tomcatä¸è¬é½è½éåä½ çè¦æ±ãä¸é¢æ¯ä¸ç³»åå ³äºTomcatçé ç½®æå·§ï¼è¿äºæå·§æºèªäºæç书ï¼ãTomcatæå¨æåãï¼å¸æå¯¹ä½ ææ帮å©ãââ Jason Brittain
1ï¼ é 置系ç»ç®¡çï¼Admin Web Applicationï¼
大å¤æ°åä¸åçJ2EEæå¡å¨é½æä¾ä¸ä¸ªåè½å¼ºå¤§ç管ççé¢ï¼ä¸å¤§é½éç¨æäºç解çWebåºç¨çé¢ãTomcatæç §èªå·±çæ¹å¼ï¼åæ ·æä¾ä¸ä¸ªæçç管çå·¥å ·ï¼å¹¶ä¸ä¸æ¯«ä¸éäºé£äºåä¸åçç«äºå¯¹æãTomcatçAdmin Web Applicationæåå¨4.1çæ¬æ¶åºç°ï¼å½æ¶çåè½å æ¬ç®¡çcontextãdata sourceãuserågroupçãå½ç¶ä¹å¯ä»¥ç®¡çååå§ååæ°ï¼userãgroupãroleçå¤ç§æ°æ®åºç®¡ççãå¨åç»ççæ¬ä¸ï¼è¿äºåè½å°å¾å°å¾å¤§çæ©å±ï¼ä½ç°æçåè½å·²ç»é常å®ç¨äºã
Admin Web Application被å®ä¹å¨èªå¨é¨ç½²æ件ï¼CATALINA_BASE/webapps/admin.xml ã
ï¼è¯è 注ï¼CATALINA_BASEå³tomcatå®è£ ç®å½ä¸çserverç®å½ï¼
ä½ å¿ é¡»ç¼è¾è¿ä¸ªæ件ï¼ä»¥ç¡®å®Contextä¸çdocBaseåæ°æ¯ç»å¯¹è·¯å¾ãä¹å°±æ¯è¯´ï¼CATALINA_BASE/webapps/admin.xml çè·¯å¾æ¯ç»å¯¹è·¯å¾ãä½ä¸ºå¦å¤ä¸ç§éæ©ï¼ä½ ä¹å¯ä»¥å é¤è¿ä¸ªèªå¨é¨ç½²æ件ï¼èå¨server.xmlæ件ä¸å»ºç«ä¸ä¸ªAdmin Web Applicationçcontextï¼æææ¯ä¸æ ·çãä½ ä¸è½ç®¡çAdmin Web Applicationè¿ä¸ªåºç¨ï¼æ¢èè¨ä¹ï¼é¤äºå é¤CATALINA_BASE/webapps/admin.xml ï¼ä½ å¯è½ä»ä¹é½åä¸äºã
å¦æä½ ä½¿ç¨UserDatabaseRealmï¼é»è®¤ï¼ï¼ä½ å°éè¦æ·»å ä¸ä¸ªuser以åä¸ä¸ªroleå°CATALINA_BASE/conf/tomcat-users.xml æ件ä¸ãä½ ç¼è¾è¿ä¸ªæ件ï¼æ·»å ä¸ä¸ªåå«âadminâçrole å°è¯¥æ件ä¸ï¼å¦ä¸ï¼
<role name="admin"/>
ä½ åæ ·éè¦æä¸ä¸ªç¨æ·ï¼å¹¶ä¸è¿ä¸ªç¨æ·çè§è²æ¯âadminâã象åå¨çç¨æ·é£æ ·ï¼æ·»å ä¸ä¸ªç¨æ·ï¼æ¹åå¯ç ä½¿å ¶æ´å å®å ¨ï¼ï¼
<user name="admin" password="deep_dark_secret" roles="admin"/>
å½ä½ å®æè¿äºæ¥éª¤åï¼è¯·éæ°å¯å¨Tomcatï¼è®¿é® -->
<Host name="www.example.com" appBase="/home/example/webapp">
<Context path="" docBase="."/>
</Host>
</Engine>
</Service>
</Server>
Tomcatçserver.xmlæ件ï¼å¨åå§ç¶æä¸ï¼åªå æ¬ä¸ä¸ªèæ主æºï¼ä½æ¯å®å®¹æ被æ©å å°æ¯æå¤ä¸ªèæ主æºãå¨åé¢çä¾åä¸å±ç¤ºçæ¯ä¸ä¸ªç®åçserver.xmlçæ¬ï¼å ¶ä¸ç²ä½é¨åå°±æ¯ç¨äºæ·»å ä¸ä¸ªèæ主æºãæ¯ä¸ä¸ªHostå ç´ å¿ é¡»å æ¬ä¸ä¸ªæå¤ä¸ªcontextå ç´ ï¼æå å«çcontextå ç´ ä¸å¿ é¡»æä¸ä¸ªæ¯é»è®¤çcontextï¼è¿ä¸ªé»è®¤çcontextçæ¾ç¤ºè·¯å¾åºè¯¥ä¸ºç©ºï¼ä¾å¦ï¼path=ââï¼ã
5ï¼é ç½®åºç¡éªè¯ï¼Basic Authenticationï¼
容å¨ç®¡çéªè¯æ¹æ³æ§å¶çå½ç¨æ·è®¿é®åä¿æ¤çwebåºç¨èµæºæ¶ï¼å¦ä½è¿è¡ç¨æ·ç身份é´å«ãå½ä¸ä¸ªwebåºç¨ä½¿ç¨äºBasic Authenticationï¼BASICåæ°å¨web.xmlæ件ä¸auto-methodå ç´ ä¸è®¾ç½®ï¼ï¼èæç¨æ·è®¿é®åä¿æ¤çwebåºç¨æ¶ï¼Tomcatå°éè¿HTTP Basic Authenticationæ¹å¼ï¼å¼¹åºä¸ä¸ªå¯¹è¯æ¡ï¼è¦æ±ç¨æ·è¾å ¥ç¨æ·ååå¯ç ãå¨è¿ç§éªè¯æ¹æ³ä¸ï¼ææå¯ç å°è¢«ä»¥ä½çç¼ç æ¹å¼å¨ç½ç»ä¸ä¼ è¾ã
注æï¼ä½¿ç¨Basic Authenticationéè¿è¢«è®¤ä¸ºæ¯ä¸å®å ¨çï¼å 为å®æ²¡æ强å¥çå å¯æ¹æ³ï¼é¤éå¨å®¢æ·ç«¯åæå¡å¨ç«¯é½ä½¿ç¨HTTPSæè å ¶ä»å¯ç å å¯ç æ¹å¼ï¼æ¯å¦ï¼å¨ä¸ä¸ªèæç§äººç½ç»ä¸ï¼ãè¥æ²¡æé¢å¤çå å¯æ¹æ³ï¼ç½ç»ç®¡çåå°è½å¤æªè·ï¼æ滥ç¨ï¼ç¨æ·çå¯ç ãä½æ¯ï¼å¦æä½ æ¯åå¼å§ä½¿ç¨Tomcatï¼æè ä½ æ³å¨ä½ çwebåºç¨ä¸æµè¯ä¸ä¸åºäºå®¹å¨çå®å ¨ç®¡çï¼Basic Authenticationè¿æ¯é常æäºè®¾ç½®å使ç¨çãåªéè¦æ·»å <security-constraint>å<login-config>两个å ç´ å°ä½ çwebåºç¨çweb.xmlæ件ä¸ï¼å¹¶ä¸å¨CATALINA_BASE/conf/tomcat-users.xml æ件ä¸æ·»å éå½ç<role>å<user>å³å¯ï¼ç¶åéæ°å¯å¨Tomcatã
ä¸é¢ä¾åä¸çweb.xmlæèªä¸ä¸ªä¿±ä¹é¨ä¼åç½ç«ç³»ç»ï¼è¯¥ç³»ç»ä¸åªæmemberç®å½è¢«ä¿æ¤èµ·æ¥ï¼å¹¶ä½¿ç¨Basic Authenticationè¿è¡èº«ä»½éªè¯ã请注æï¼è¿ç§æ¹å¼å°ææç代æ¿Apache webæå¡å¨ä¸ç.htaccessæ件ã
<!--
Define the Members-only area, by defining
a "Security Constraint" on this Application, and
mapping it to the subdirectory (URL) that we want
to restrict.
-->
<security-constraint>
<web-resource-collection>
<web-resource-name>
Entire Application
</web-resource-name>
<url-pattern>/members/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<!-- Define the Login Configuration for this Application -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Club Members-only Area</realm-name>
</login-config>
6ï¼é ç½®åç¹ç»å½ï¼Single Sign-Onï¼
ä¸æ¦ä½ 设置äºrealmåéªè¯çæ¹æ³ï¼ä½ å°±éè¦è¿è¡å®é çç¨æ·ç»å½å¤çãä¸è¬è¯´æ¥ï¼å¯¹ç¨æ·èè¨ç»å½ç³»ç»æ¯ä¸ä»¶å¾éº»ç¦çäºæ ï¼ä½ å¿ é¡»å°½éåå°ç¨æ·ç»å½éªè¯ç次æ°ãä½ä¸ºç¼ºççæ åµï¼å½ç¨æ·ç¬¬ä¸æ¬¡è¯·æ±åä¿æ¤çèµæºæ¶ï¼æ¯ä¸ä¸ªwebåºç¨é½ä¼è¦æ±ç¨æ·ç»å½ãå¦æä½ è¿è¡äºå¤ä¸ªwebåºç¨ï¼å¹¶ä¸æ¯ä¸ªåºç¨é½éè¦è¿è¡åç¬çç¨æ·éªè¯ï¼é£è¿çèµ·æ¥å°±æç¹åä½ å¨ä¸ä½ çç¨æ·ææãç¨æ·ä»¬ä¸ç¥éææ ·æè½æå¤ä¸ªå离çåºç¨æ´åæä¸ä¸ªåç¬çç³»ç»ï¼ææä»ä»¬ä¹å°±ä¸ç¥éä»ä»¬éè¦è®¿é®å¤å°ä¸ªä¸åçåºç¨ï¼åªæ¯å¾è¿·æï¼ä¸ºä»ä¹æ»è¦ä¸åçç»å½ã
Tomcat 4çâsingle sign-onâç¹æ§å 许ç¨æ·å¨è®¿é®åä¸èæ主æºä¸ææwebåºç¨æ¶ï¼åªéç»å½ä¸æ¬¡ã为äºä½¿ç¨è¿ä¸ªåè½ï¼ä½ åªéè¦å¨Hostä¸æ·»å ä¸ä¸ªSingleSignOn Valveå ç´ å³å¯ï¼å¦ä¸æ示ï¼
<Valve className="org.apache.catalina.authenticator.SingleSignOn"
debug="0"/>
å¨Tomcatåå§å®è£ åï¼server.xmlç注ééé¢å æ¬SingleSignOn Valveé ç½®çä¾åï¼ä½ åªéè¦å»æ注éï¼å³å¯ä½¿ç¨ãé£ä¹ï¼ä»»ä½ç¨æ·åªè¦ç»å½è¿ä¸ä¸ªåºç¨ï¼å对äºåä¸èæ主æºä¸çææåºç¨åæ ·ææã
使ç¨single sign-on valveæä¸äºéè¦çéå¶ï¼
1> valueå¿ é¡»è¢«é ç½®ååµå¥å¨ç¸åçHostå ç´ éï¼å¹¶ä¸ææéè¦è¿è¡åç¹éªè¯çwebåºç¨ï¼å¿ é¡»éè¿contextå ç´ å®ä¹ï¼é½ä½äºè¯¥Hostä¸ã
2> å æ¬å ±äº«ç¨æ·ä¿¡æ¯çrealmå¿ é¡»è¢«è®¾ç½®å¨åä¸çº§Hostä¸æè åµå¥ä¹å¤ã
3> ä¸è½è¢«contextä¸çrealmè¦çã
4> 使ç¨åç¹ç»å½çwebåºç¨æ好使ç¨ä¸ä¸ªTomcatçå ç½®çéªè¯æ¹å¼ï¼è¢«å®ä¹å¨web.xmlä¸ç<auth-method>ä¸ï¼ï¼è¿æ¯èªå®ä¹çéªè¯æ¹å¼å¼ºï¼Tomcatå ç½®ççéªè¯æ¹å¼å æ¬basicãdigestãformåclient-certã
5> å¦æä½ ä½¿ç¨åç¹ç»å½ï¼è¿å¸æéæä¸ä¸ªç¬¬ä¸æ¹çwebåºç¨å°ä½ çç½ç«ä¸æ¥ï¼å¹¶ä¸è¿ä¸ªæ°çwebåºç¨ä½¿ç¨å®èªå·±çéªè¯æ¹å¼ï¼èä¸ä½¿ç¨å®¹å¨ç®¡çå®å ¨ï¼é£ä½ åºæ¬ä¸å°±æ²¡æäºãä½ çç¨æ·æ¯æ¬¡ç»å½åæ¥ææåºç¨æ¶éè¦ç»å½ä¸æ¬¡ï¼å¹¶ä¸å¨è¯·æ±æ°ç第ä¸æ¹åºç¨æ¶è¿å¾åç»å½ä¸æ¬¡ãå½ç¶ï¼å¦æä½ æ¥æè¿ä¸ªç¬¬ä¸æ¹webåºç¨çæºç ï¼èä½ åæ¯ä¸ä¸ªç¨åºåï¼ä½ å¯ä»¥ä¿®æ¹å®ï¼ä½é£ææä¹ä¸å®¹æåã
6> åç¹ç»å½éè¦ä½¿ç¨cookiesã
7ï¼é ç½®ç¨æ·å®å¶ç®å½ï¼Customized User Directoresï¼
ä¸äºç«ç¹å 许个å«ç¨æ·å¨æå¡å¨ä¸åå¸ç½é¡µãä¾å¦ï¼ä¸æ大å¦çå¦é¢å¯è½æ³ç»æ¯ä¸ä½å¦çä¸ä¸ªå ¬å ±åºåï¼æè æ¯ä¸ä¸ªISPå¸æç»ä¸äºweb空é´ç»ä»ç客æ·ï¼ä½è¿åä¸æ¯èæ主æºãå¨è¿ç§æ åµä¸ï¼ä¸ä¸ªå ¸åçæ¹æ³å°±æ¯å¨ç¨æ·ååé¢å ä¸ä¸ªç¹æ®å符ï¼~ï¼ï¼ä½ä¸ºæ¯ä½ç¨æ·çç½ç«ï¼æ¯å¦ï¼
/~username
Tomcatæä¾ä¸¤ç§æ¹æ³å¨ä¸»æºä¸æ å°è¿äºä¸ªäººç½ç«ï¼ä¸»è¦ä½¿ç¨ä¸å¯¹ç¹æ®çListenerå ç´ ãListenerçclassNameå±æ§åºè¯¥æ¯org.apache.catalina.startup.UserConfigï¼userClasså±æ§åºè¯¥æ¯å 个æ å°ç±»ä¹ä¸ãå¦æä½ çç³»ç»æ¯Unixï¼å®å°æä¸ä¸ªæ åç/etc/passwdæ件ï¼è¯¥æ件ä¸çå¸å·è½å¤è¢«è¿è¡ä¸çTomcatå¾å®¹æç读åï¼è¯¥æ件æå®äºç¨æ·ç主ç®å½ï¼ä½¿ç¨PasswdUserDatabase æ å°ç±»ã
<Listener className="org.apache.catalina.startup.UserConfig"
directoryName="public_html"
userClass="org.apache.catalina.startup.PasswdUserDatabase"/>
webæ件éè¦æ¾ç½®å¨å/home/users/ian/public_html æè /users/jbrittain/public_htmlä¸æ ·çç®å½ä¸é¢ãå½ç¶ä½ ä¹å¯ä»¥æ¹åpublic_html å°å ¶ä»ä»»ä½åç®å½ä¸ã
å®é ä¸ï¼è¿ä¸ªç¨æ·ç®å½æ ¹æ¬ä¸ä¸å®éè¦ä½äºç¨æ·ä¸»ç®å½ä¸éé¢ãå¦æä½ æ²¡æä¸ä¸ªå¯ç æ件ï¼ä½ä½ åæ³æä¸ä¸ªç¨æ·åæ å°å°å ¬å ±çå/homeä¸æ ·ç®å½çåç®å½éé¢ï¼åå¯ä»¥ä½¿ç¨HomesUserDatabaseç±»ã
<Listener className="org.apache.catalina.startup.UserConfig"
directoryName="public_html" homeBase="/home"
userClass="org.apache.catalina.startup.HomesUserDatabase"/>
è¿æ ·ä¸æ¥ï¼webæ件就å¯ä»¥ä½äºå/home/ian/public_html æè /home/jasonb/public_htmlä¸æ ·çç®å½ä¸ãè¿ç§å½¢å¼å¯¹Windowsèè¨æ´å æå©ï¼ä½ å¯ä»¥ä½¿ç¨ä¸ä¸ªåc:\homeè¿æ ·çç®å½ã
è¿äºListenerå ç´ ï¼å¦æåºç°ï¼åå¿ é¡»å¨Hostå ç´ éé¢ï¼èä¸è½å¨contextå ç´ éé¢ï¼å 为å®ä»¬é½ç¨åºç¨äºHostæ¬èº«ã
8ï¼å¨Tomcatä¸ä½¿ç¨CGIèæ¬
Tomcat主è¦æ¯ä½ä¸ºServlet/JSP容å¨ï¼ä½å®ä¹æ许å¤ä¼ ç»webæå¡å¨çæ§è½ãæ¯æéç¨ç½å ³æ¥å£ï¼Common Gateway Interfaceï¼å³CGIï¼å°±æ¯å ¶ä¸ä¹ä¸ï¼CGIæä¾ä¸ç»æ¹æ³å¨ååºæµè§å¨è¯·æ±æ¶è¿è¡ä¸äºæ©å±ç¨åºãCGIä¹æ以被称为éç¨ï¼æ¯å 为å®è½å¨å¤§å¤æ°ç¨åºæèæ¬ä¸è¢«è°ç¨ï¼å æ¬ï¼Perlï¼Pythonï¼awkï¼Unix shell scriptingçï¼çè³å æ¬Javaãå½ç¶ï¼ä½ 大æ¦ä¸ä¼æä¸ä¸ªJavaåºç¨ç¨åºå½ä½CGIæ¥è¿è¡ï¼æ¯ç«è¿æ ·å¤ªè¿åå§ãä¸è¬èè¨ï¼å¼åServletæ»è¦æ¯CGIå ·ææ´å¥½çæçï¼å 为å½ç¨æ·ç¹å»ä¸ä¸ªé¾æ¥æä¸ä¸ªæé®æ¶ï¼ä½ ä¸éè¦ä»æä½ç³»ç»å±å¼å§è¿è¡å¤çã
Tomcatå æ¬ä¸ä¸ªå¯éçCGI Servletï¼å è®¸ä½ è¿è¡éçä¸æ¥çCGIèæ¬ã
为äºä½¿Tomcatè½å¤è¿è¡CGIï¼ä½ å¿ é¡»åå¦ä¸å 件äºï¼
1. æservlets-cgi.renametojar ï¼å¨CATALINA_HOME/server/lib/ç®å½ä¸ï¼æ¹å为servlets-cgi.jarãå¤çCGIçservletåºè¯¥ä½äºTomcatçCLASSPATHä¸ã
2. å¨TomcatçCATALINA_BASE/conf/web.xml æ件ä¸ï¼æå ³äº<servlet-name> CGIçé£æ®µç注éå»æï¼é»è®¤æ åµä¸ï¼è¯¥æ®µä½äºç¬¬è¡ï¼ã
3. åæ ·ï¼å¨TomcatçCATALINA_BASE/conf/web.xmlæ件ä¸ï¼æå ³äºå¯¹CGIè¿è¡æ å°çé£æ®µç注éå»æï¼é»è®¤æ åµä¸ï¼è¯¥æ®µä½äºç¬¬è¡ï¼ã注æï¼è¿æ®µå 容æå®äºHTMLé¾æ¥å°CGIèæ¬ç访é®æ¹å¼ã
4. ä½ å¯ä»¥æCGIèæ¬æ¾ç½®å¨WEB-INF/cgi ç®å½ä¸ï¼æ³¨æï¼WEB-INFæ¯ä¸ä¸ªå®å ¨çå°æ¹ï¼ä½ å¯ä»¥æä¸äºä¸æ³è¢«ç¨æ·çè§æåºäºå®å ¨èèä¸æ³æ´é²çæ件æ¾å¨æ¤å¤ï¼ï¼æè ä½ ä¹å¯ä»¥æCGIèæ¬æ¾ç½®å¨contextä¸çå ¶ä»ç®å½ä¸ï¼å¹¶ä¸ºCGI Servletè°æ´cgiPathPrefixåå§ååæ°ãè¿å°±æå®çCGI Servletçå®é ä½ç½®ï¼ä¸ä¸è½ä¸ä¸ä¸æ¥æå®çURLéåã
5. éæ°å¯å¨Tomcatï¼ä½ çCGIå°±å¯ä»¥è¿è¡äºã
å¨Tomcatä¸ï¼CGIç¨åºç¼ºçæ¾ç½®å¨WEB-INF/cgiç®å½ä¸ï¼æ£å¦åé¢ææ示çé£æ ·ï¼WEB-INFç®å½åä¿æ¤çï¼éè¿å®¢æ·ç«¯çæµè§å¨æ æ³çª¥æ¢å°å ¶ä¸å 容ï¼æ以对äºæ¾ç½®å«æå¯ç æå ¶ä»ææä¿¡æ¯çCGIèæ¬èè¨ï¼è¿æ¯ä¸ä¸ªé常好çå°æ¹ã为äºå ¼å®¹å ¶ä»æå¡å¨ï¼å°½ç®¡ä½ ä¹å¯ä»¥æCGIèæ¬ä¿åå¨ä¼ ç»ç/cgi-binç®å½ï¼ä½è¦ç¥éï¼å¨è¿äºç®å½ä¸çæ件æå¯è½è¢«ç½ä¸å¥½å¥çå²æµªè çå°ãå¦å¤ï¼å¨Unixä¸ï¼è¯·ç¡®å®è¿è¡Tomcatçç¨æ·ææ§è¡CGIèæ¬çæéã
9ï¼æ¹åTomcatä¸çJSPç¼è¯å¨ï¼JSP Compilerï¼
å¨Tomcat 4.1ï¼ææ´é«çæ¬ï¼å¤§æ¦ï¼ï¼JSPçç¼è¯ç±å å«å¨Tomcatéé¢çAntç¨åºæ§å¶å¨ç´æ¥æ§è¡ãè¿å¬èµ·æ¥æä¸ç¹ç¹å¥æªï¼ä½è¿æ£æ¯Antææ为ä¹çä¸é¨åï¼æä¸ä¸ªAPIææ¡£æ导å¼åè å¨æ²¡æå¯å¨ä¸ä¸ªæ°çJVMçæ åµä¸ï¼ä½¿ç¨Antãè¿æ¯ä½¿ç¨Antè¿è¡Javaå¼åçä¸å¤§ä¼å¿ãå¦å¤ï¼è¿ä¹æå³çä½ ç°å¨è½å¤å¨Antä¸ä½¿ç¨ä»»ä½javacæ¯æçç¼è¯æ¹å¼ï¼è¿éæä¸ä¸ªå ³äºApache Ant使ç¨æåçjavac pageå表ã使ç¨èµ·æ¥æ¯å®¹æçï¼å ä¸ºä½ åªéè¦å¨<init-param> å ç´ ä¸å®ä¹ä¸ä¸ªååå«âcompilerâï¼å¹¶ä¸å¨valueä¸æä¸ä¸ªæ¯æç¼è¯çç¼è¯å¨ååï¼ç¤ºä¾å¦ä¸ï¼
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>
org.apache.jasper.servlet.JspServlet
</servlet-class>
<init-param>
<param-name>logVerbosityLevel</param-name>
<param-value>WARNING</param-value>
</init-param>
<init-param>
<param-name>compiler</param-name>
<param-value>jikes</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
å½ç¶ï¼ç»åºçç¼è¯å¨å¿ 须已ç»å®è£ å¨ä½ çç³»ç»ä¸ï¼å¹¶ä¸CLASSPATHå¯è½éè¦è®¾ç½®ï¼é£å¤å³äºä½ éæ©çæ¯ä½ç§ç¼è¯å¨ã
ï¼éå¶ç¹å®ä¸»æºè®¿é®ï¼Restricting Access to Specific Hostsï¼
ææ¶ï¼ä½ å¯è½æ³éå¶å¯¹Tomcat webåºç¨ç访é®ï¼æ¯å¦ï¼ä½ å¸æåªæä½ æå®ç主æºæIPå°åå¯ä»¥è®¿é®ä½ çåºç¨ãè¿æ ·ä¸æ¥ï¼å°±åªæé£äºæå®çç客æ·ç«¯å¯ä»¥è®¿é®æå¡çå 容äºã为äºå®ç°è¿ç§ææï¼Tomcatæä¾äºä¸¤ä¸ªåæ°ä¾ä½ é ç½®ï¼RemoteHostValve åRemoteAddrValveã
éè¿é ç½®è¿ä¸¤ä¸ªåæ°ï¼å¯ä»¥è®©ä½ è¿æ»¤æ¥èªè¯·æ±ç主æºæIPå°åï¼å¹¶å 许ææç»åªäºä¸»æº/IPãä¸ä¹ç±»ä¼¼çï¼å¨Apacheç/products/jsp/tomcat/)ï¼å æ¤å ¶è¶æ¥è¶å¤çåå°è½¯ä»¶å ¬å¸åå¼å人åçåç±ãservletåjspçææ°è§èé½å¯ä»¥å¨tomcatçæ°çæ¬ä¸å¾å°å®ç°ã
äºï¼å®è£ åé ç½®
tomcatææ°çæ¬ä¸º4.0.1ï¼è¿ä¸ªçæ¬ç¨äºä¸ä¸ªæ°çservlet容å¨Catalinaï¼å®æ´çå®ç°äºservlet2.3åjsp1.2è§èã注æå®è£ ä¹åä½ çç³»ç»å¿ é¡»å®è£ äºjdk1.2以ä¸çæ¬ã
(ä¸)ï¼å®è£
1ï¼windowså¹³å°
ä»tomcatç½ç«ä¸è½½jakarta-tomcat-4.0.1.exeï¼æç §ä¸è¬çwindowsç¨åºå®è£ æ¥éª¤å³å¯å®è£ 好tomcat,å®è£ æ¶å®ä¼èªå¨å¯»æ¾ä½ çjdkåjreçä½ç½®ã
2ï¼linuxå¹³å°
ä¸è½½jakarta-tomcat-4.0.1.tar.gzï¼å°å ¶è§£åå°ä¸ä¸ªç®å½ã
(äº)ï¼é ç½®
è¿è¡tomcatéè¦è®¾ç½®JAVA_HOMEåé
set JAVA_HOME=c:/jdk (winï¼å¨msdosæ¹å¼ä¸ä½¿ç¨ï¼æè æ¾å ¥autoexec.batä¸)
export JAVA_HOME=/usr/local/jdk (linuxä¸ä½¿ç¨ï¼æ¾å°/etc/bashrcæè /etc/profileä¸)
(ä¸)ï¼è¿è¡
设置å®æ¯åå°±å¯ä»¥è¿è¡tomcatæå¡å¨äºï¼è¿å ¥tomcatçbinç®å½ï¼winä¸ç¨startupå¯å¨tomcatï¼linuxä¸ç¨startup.shï¼ç¸åºçå ³étomcatçå½ä»¤ä¸ºshutdownåshutdown.shã
å¯å¨åå¯ä»¥å¨æµè§å¨ä¸è¾å ¥http://localhost:/æµè¯ï¼ç±äºtomcatæ¬èº«å ·æwebæå¡å¨çåè½ï¼å æ¤æ们ä¸å¿ å®è£ apacheï¼å½ç¶å ¶ä¹å¯ä»¥ä¸apacheéæå°ä¸èµ·ï¼ä¸é¢ä¼ä»ç»ã
ä¸é¢ä½ å¯ä»¥æµè¯å ¶èªå¸¦çjspåservlet示ä¾ã
ä¸ï¼åºç¨
(ä¸):ç®å½ç»æ
tomcatçç®å½ç»æå¦ä¸ï¼ ç®å½å ç®ä»
bin åæ¾å¯å¨åå ³étomcatèæ¬
conf å å«ä¸åçé ç½®æ件,server.xml(Tomcatç主è¦é ç½®æ件)åweb.xml
work åæ¾jspç¼è¯å产ççclassæ件
webapp åæ¾åºç¨ç¨åºç¤ºä¾ï¼ä»¥åä½ è¦é¨ç½²çåºç¨
å¦å¤è¿æåEclipseãWebSphereãWebLogicè¿æ ·çIDEç¯å¢ï¼å°±ä¸ä¸ä¸ä»ç»äºï¼