项目登陆使用https,其他使用http的方式一点尝试

来源:互联网 发布:北京数据恢复推荐 编辑:程序博客网 时间:2024/05/18 03:18

由于项目需要,在登录的时候需要使用https对用户的数据进行加密,登录完后跳转的其他页面时使用http方式访问;

根据项目实际使用的安全框架shiro,在网上查了很多资料,使用步骤如下:

1.在tomcat中server.xml中配置ssl,可以默认使用443端口

生成一个keystore文件,后面配置tomcat会用到

[html] view plain copy
  1. <Connector port="80" protocol="HTTP/1.1"  
  2.               connectionTimeout="20000"  
  3.               redirectPort="443" />  
  4.      
  5.    <Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"  
  6.               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"  
  7.               clientAuth="false" sslProtocol="TLS" keystoreFile="D:\localhost.keystore" keystorePass="123456"/>  
  8.   
  9.    <Connector port="8009" protocol="AJP/1.3" redirectPort="443" />  
2.使用shiro对登陆路径进行过滤

在spring_shiro.xml中增加

[html] view plain copy
  1. <bean id="sslFilter" class="org.apache.shiro.web.filter.authz.SslFilter">  
  2.         <property name="port" value="443"/>  
  3.     </bean>  

在shiro主过滤器配置中新增
[html] view plain copy
  1. <property name="filters">    
  2.             <map>    
  3.                ......  
  4.                 <entry key="ssl" value-ref="sslFilter"/>  
[html] view plain copy
  1. <span style="white-space:pre">        </span>......  
  2.             </map>    
  3.         </property>  
在过滤链中配置路径过滤

login/login=ssl

这样就保证在点击登陆时使用https访问

3.在登陆完后怎么重定向到http且session不丢失呢

重定向时,需要重新把sessionId重新放到cookie中,具体原因可查https http 跨域的问题

[java] view plain copy
  1. Cookie cookie = new Cookie("JSESSIONID", request.getSession().getId());  
  2.         cookie.setPath(request.getContextPath());  
  3.         response.addCookie(cookie);  
这样就可登录使用https,其他链接使用http
0 0
原创粉丝点击