使用cookie+Filter实现单点登录

一、什么是单点登录(SSO)? 

   单点登录其实就是实现这么一个功能。例如你登陆了www.bbs.njupt.com这个网站,当你再登陆www.news.njupt.com这个网站时,

就不需要再登陆了。以上两个网站一个很大的相似点,就是都有相同的域名.njupt.com 。


二、单点登录的代码实现

1、新建一个webproject ,名为sso_bbs  

2、导包

    导入单点登录的基本jar包(2个)

3、LoginServlet

   新建一个servlet,并将其servlet/JSP Mapping url 改成 /login

代码如下:

package com.njupt.sso.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		if(userName != null && password != null){
			if(userName.equals(password)){//登录成功,实际应查询数据库
				request.getSession().setAttribute("user", userName);
				
				//向客户端写入cookie
				Cookie c = new Cookie("sso",userName);
				
				c.setMaxAge(3600);//1小时
				c.setDomain(".njupt.com");//www.bbs.njupt.com www.news.njupt.com
				c.setPath("/");
				
				response.addCookie(c);
			}
		}
		
		response.sendRedirect(request.getContextPath() + "/index.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}


4、修改host文件

到C:\Windows\System32\drivers\etc目录下找到名为host文件,并在其中加上以下代码:

127.0.0.1        localhost
127.0.0.1        www.bbs.njupt.com
127.0.0.1        www.news.njupt.com  

5、server.xml

到tomcat的安装目录(E:\开发者工具\apache-tomcat-6.0.37-windows-x86\apache-tomcat-6.0.37\conf)下找到名为server.xml的文件

在该文件中加上以下代码:

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->

      </Host>

	   <Host name="www.bbs.njupt.com"  appBase="bbs">
       </Host>

	   <Host name="www.news.njupt.com"  appBase="news">

6、将sso_bbs项目中的webroot拷贝一份到tomcat的安装目录下的bbs、news文件夹,并改名为ROOT(因为服务器启动时会默认在ROOT文件夹中

寻找一个名为index.jsp的文件).如下图所示:



7、AutoLoginFilter

其功能主要是实现类似于一些网站中的"·····天免登陆"的功能

代码如下:

package com.njupt.sso.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class AutoLoginFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
		
		if(request.getSession().getAttribute("user")== null){
			Cookie[] cs = request.getCookies();

			if (cs != null && cs.length > 0) {
				for (Cookie c : cs) {
					String cName = c.getName();
					if (cName.equals("sso")) {
						String userName = c.getValue();
						request.getSession().setAttribute("user", userName);
					}
				}
			}
		}

		chain.doFilter(request, resp);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}


8、web.xml

在web.xml中加上以下代码:

<filter>
		<filter-name>autoLogin</filter-name>
		<filter-class>com.njupt.sso.filter.AutoLoginFilter</filter-class>
	</filter>	
	<filter-mapping>
		<filter-name>autoLogin</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

9、注意,这是需要重新考一下webroot到tomcat安装目录中的bbs、news文件夹


10、这时便可以在地址栏中输入http://www.bbs.njupt.com:8080/,登陆成功以后,输入http://www.news.njupt.com:8080/时便不需要再登陆


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
单点登录(Single Sign-On,简称SSO)是指用户只需要录一次,就可以访问多个应用系统。Java中可以使用Spring Security框架来实现SSO功能。 下面是一个简单的示例,假设我们有两个应用系统A和B,用户只需要在其中一个系统中录,就可以在另一个系统中访问受保护的资源。 1. 配置系统A和系统B的Spring Security 首先,我们需要在两个系统中都配置Spring Security。可以参考Spring Security官方文档进行配置。 2. 配置认证服务器 我们需要创建一个认证服务器,用于管理用户的身份验证和授权信息。可以使用Spring Security OAuth2来实现。在认证服务器中,我们需要定义一个OAuth2认证服务器和一个资源服务器。 在OAuth2认证服务器中,我们需要定义一个ClientDetailsService,用于管理客户端信息。在资源服务器中,我们需要定义一个ResourceServerConfigurerAdapter,用于配置资源服务器的安全策略。 3. 在系统A和系统B中配置认证服务器 在系统A和系统B中,我们需要将OAuth2认证服务器配置为远程认证服务器,并启用SSO功能。可以使用Spring Security OAuth2提供的@EnableOAuth2Sso注解来启用SSO功能。 4. 配置单点注销 如果用户在一个系统中注销,我们需要将注销信息通知其他系统,并使其他系统也注销该用户。可以使用Spring Security OAuth2提供的@EnableOAuth2Sso注解和@EnableOAuth2Client注解来实现此功能。 5. 配置单点登录后跳转的页面 最后,我们需要在认证服务器中配置单点登录后跳转的页面。可以使用Spring Security OAuth2提供的loginPage()方法来设置录页面。 参考示例代码: 认证服务器配置: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/api/**").authenticated(); } } ``` 系统A和系统B的配置: ```java @SpringBootApplication @EnableOAuth2Sso public class Application extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.antMatcher("/**").authorizeRequests().antMatchers("/login**", "/error**").permitAll().anyRequest() .authenticated().and().logout().logoutUrl("/logout").logoutSuccessUrl("/") .deleteCookies("JSESSIONID").invalidateHttpSession(true).and().csrf().csrfTokenRepository(csrfTokenRepository()) .and().addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/static/**"); } @Bean public FilterRegistrationBean<CsrfFilter> csrfFilter() { FilterRegistrationBean<CsrfFilter> registration = new FilterRegistrationBean<>(); registration.setFilter(new CsrfFilter(csrfTokenRepository())); registration.addUrlPatterns("/*"); return registration; } private CsrfTokenRepository csrfTokenRepository() { HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); repository.setSessionAttributeName("_csrf"); return repository; } private Filter csrfHeaderFilter() { return new OncePerRequestFilter() { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); if (csrf != null) { Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); String token = csrf.getToken(); if (cookie == null || token != null && !token.equals(cookie.getValue())) { cookie = new Cookie("XSRF-TOKEN", token); cookie.setPath("/"); response.addCookie(cookie); } } filterChain.doFilter(request, response); } }; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值