?
目錄貼: ?跟我學(xué)Shiro目錄貼
?
對(duì)于 SSL 的支持, Shiro 只是判斷當(dāng)前 url 是否需要 SSL 登錄,如果需要自動(dòng)重定向到 https 進(jìn)行訪問。
?
首先生成數(shù)字證書,生成證書到 D:\localhost.keystore
使用 JDK 的 keytool 命令,生成證書(包含證書 / 公鑰 / 私鑰)到 D:\localhost.keystore :
keytool -genkey -keystore "D:\localhost.keystore" -alias localhost -keyalg RSA
輸入密鑰庫口令 :
再次輸入新口令 :
您的名字與姓氏是什么 ?
? [Unknown]:? localhost
您的組織單位名稱是什么 ?
? [Unknown]:? sishuok.com
您的組織名稱是什么 ?
? [Unknown]:? sishuok.com
您所在的城市或區(qū)域名稱是什么 ?
? [Unknown]:? beijing
您所在的省 / 市 / 自治區(qū)名稱是什么 ?
? [Unknown]:? beijing
該單位的雙字母國家 / 地區(qū)代碼是什么 ?
? [Unknown]:? cn
CN=localhost, OU=sishuok.com, O=sishuok.com, L=beijing, ST=beijing, C=cn 是否正確
?
? [ 否 ]:? y
?
輸入 ?<localhost>? 的密鑰口令
??????? ( 如果和密鑰庫口令相同 ,? 按回車 ):
再次輸入新口令 :
?
通過如上步驟,生成證書到 D:\ localhost.keystore ;
?
然后設(shè)置 tomcat 下的 server.xml
此處使用了 apache-tomcat-7.0.40 版本,打開 conf/server.xml ,找到:
?
<!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->?
替換為 ??
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:\localhost.keystore" keystorePass="123456"/>?
keystorePass 就是生成 keystore 時(shí)設(shè)置的密碼。
?
添加 SSL 到配置文件( spring-shiro-web.xml )
此處使用了和十三章一樣的代碼:
<bean id="sslFilter" class="org.apache.shiro.web.filter.authz.SslFilter"> <property name="port" value="8443"/> </bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> …… <property name="filters"> <util:map> <entry key="authc" value-ref="formAuthenticationFilter"/> <entry key="ssl" value-ref="sslFilter"/> </util:map> </property> <property name="filterChainDefinitions"> <value> /login.jsp = ssl,authc /logout = logout /authenticated.jsp = authc /** = user </value> </property> </bean>?
SslFilter默認(rèn)端口是443,此處使用了8443;“/login.jsp = ssl,authc”表示訪問登錄頁面時(shí)需要走SSL。
?
?
測試
最后把 shiro-example-chapter14 打成 war 包( mvn:package ),放到 tomcat 下的 webapps 中,啟動(dòng)服務(wù)器測試,如訪問 localhost:9080/chapter14/ ,會(huì)自動(dòng)跳轉(zhuǎn)到 https://localhost:8443/chapter14/login.jsp 。
?
如果使用 Maven Jetty 插件,可以直接如下插件配置:?
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.8.v20121106</version> <configuration> <webAppConfig> <contextPath>/${project.build.finalName}</contextPath> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8080</port> </connector> <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector"> <port>8443</port> <keystore>${project.basedir}/localhost.keystore</keystore> <password>123456</password> <keyPassword>123456</keyPassword> </connector> </connectors> </configuration> </plugin>
?
??
示例源代碼: https://github.com/zhangkaitao/shiro-example ;可加群 231889722?探討Spring/Shiro技術(shù)。
??
請(qǐng)你欣賞春天美景? http://user.qzone.qq.com/314154083/photo/V10a4ot72FxYVR?ptlang=2052
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
