使用filter cookie实现单点登录

发布时间:2024-05-30 点击:155
1.什么事单点登录
单点登录其实就是实现这么一个功能。例如你登陆了www.bbs.kite.com这个网站,当你再登陆www.news.kite.com这个网站时,
就不需要再登陆了。以上两个网站一个很大的相似点,就是都有相同的域名.kite.com 。
二、单点登录的代码实现
1、新建一个webproject ,名为sso_bbs
2.新建一个servlet
1 package kite.servlet; 2 3 import java.io.ioexception; 4 import java.io.printwriter; 5 6 import javax.servlet.servletexception; 7 import javax.servlet.http.cookie; 8 import javax.servlet.http.httpservlet; 9 import javax.servlet.http.httpservletrequest; 10 import javax.servlet.http.httpservletresponse; 11 12 public class loginservlet extends httpservlet 13 { 14 public void doget(httpservletrequest request, httpservletresponse response) 15 throws servletexception, ioexception 16 { 17 this.dopost(request, response); 18 } 19 20 public void dopost(httpservletrequest request, httpservletresponse response) 21 throws servletexception, ioexception 22 { 23 string username = request.getparameter("username"); 24 string password = request.getparameter("password"); 25 26 if(username != null && password != null) 27 { 28 if(username.equals(password)) 29 { 30 request.getsession().setattribute("user", username); 31 32 cookie cookie = new cookie("sso", username); 33 cookie.setmaxage(60*60); 34 cookie.setdomain(".kite.com"); 35 cookie.setpath("/"); 36 response.addcookie(cookie); 37 } 38 } 39 response.sendredirect(request.getcontextpath() "/index.jsp"); 40 } 41 42 }
3.修改host文件
到c:windowssystem32driversetc目录下找到名为host文件,并在其中加上以下代码:
127.0.0.1 localhost 127.0.0.1 www.bbs.kite.com 127.0.0.1 www.news.kite.com
4.修改tomcat/config/server.xml文件
1 <?xml version=\\\’1.0\\\’ encoding=\\\’utf-8\\\’?> 2 <!– 3 licensed to the apache software foundation (asf) under one or more 4 contributor license agreements. see the notice file distributed with 5 this work for additional information regarding copyright ownership. 6 the asf licenses this file to you under the apache license, version 2.0 7 (the "license"); you may not use this file except in compliance with 8 the license. you may obtain a copy of the license at 9 10 http://www.apache.org/licenses/license-2.0 11 12 unless required by applicable law or agreed to in writing, software 13 distributed under the license is distributed on an "as is" basis, 14 without warranties or conditions of any kind, either express or implied. 15 see the license for the specific language governing permissions and 16 limitations under the license. 17 –> 18 <!– note: a "server" is not itself a "container", so you may not 19 define subcomponents such as "valves" at this level. 20 documentation at /docs/config/server.html 21 –> 22 <server port="8005" shutdown="shutdown"> 23 <!– security listener. documentation at /docs/config/listeners.html 24 <listener classname="org.apache.catalina.security.securitylistener" /> 25 –> 26 <!–apr library loader. documentation at /docs/apr.html –> 27 <listener classname="org.apache.catalina.core.aprlifecyclelistener" sslengine="on" /> 28 <!–initialize jasper prior to webapps are loaded. documentation at /docs/jasper-howto.html –> 29 <listener classname="org.apache.catalina.core.jasperlistener" /> 30 <!– prevent memory leaks due to use of particular java/javax apis–> 31 <listener classname="org.apache.catalina.core.jrememoryleakpreventionlistener" /> 32 <listener classname="org.apache.catalina.mbeans.globalresourceslifecyclelistener

ai域名注册步骤是怎样的 ai域名具有哪些优势
云服务器怎么更新系统
关于租域名多少钱一个的资料
不了解这些?当心你的网站被谷歌红牌罚出!
公司网址域名到期了怎么办,应该怎么处理?
麻烦问下这个是什么意思-备案平台
怎么进行域名期限查询?域名到期后状态有什么变化?
企业定制官网的费用 建立网站时要注意什么