---
title: "waffle结合spring-security进行windows认证"
author: "yangyongByJava"
date: "2012-08-27T10:32:27Z"
tags: []
categories: ["开源软件"]
slug: "waffle-spring-security-windows"
draft: false
lastmod: "2012-08-27T10:37:14Z"
id: 1663619
url: "https://yangyongbyjava.iteye.com/blog/1663619"
---

waffle是实现Windows & Active Directory单点登录的一种方式，它能过做一切windows认证 的事情，包括  Negotiate ，NTLM和Kerberos。其实现步骤如下：

1.下载waffle所需的jar文件，下载地址[http://dblock.github.com/waffle/](http://dblock.github.com/waffle/)；

2.新建一个web项目，将waffle认证和spring-security相关的jar文件添加到web项目中，waffle所需的jar包分别为：

commons-logging-1.1.1.jar、guava-r07.jar、jna.jar、platform.jar、waffle-jacob.jar、waffle-jna.jar；

3、修改web.xml文件的配置为：

&lt;filter&gt;

&lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;

&lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;

&lt;/filter&gt;

&lt;filter-mapping&gt;

&lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;

&lt;url-pattern&gt;/\*&lt;/url-pattern&gt;

&lt;/filter-mapping&gt;

&lt;context-param&gt;

&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

&lt;param-value&gt;/WEB-INF/waffle-filter.xml&lt;/param-value&gt; 

&lt;/context-param&gt;

&lt;listener&gt;

&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;

&lt;/listener&gt;

4、在WEB-INF下建立waffle-filter.xml文件，文件内容如下：

&lt;!-- windows authentication provider --&gt;

&lt;bean id="waffleWindowsAuthProvider"

   class="waffle.windows.auth.impl.WindowsAuthProviderImpl" /&gt;

&lt;!-- collection of security filters --&gt;

&lt;bean id="negotiateSecurityFilterProvider"

                class="waffle.servlet.spi.NegotiateSecurityFilterProvider"&gt;

&lt;constructor-arg ref="waffleWindowsAuthProvider" /&gt;

&lt;/bean&gt;

&lt;bean id="basicSecurityFilterProvider" class="waffle.servlet.spi.BasicSecurityFilterProvider"&gt;

&lt;constructor-arg ref="waffleWindowsAuthProvider" /&gt;

&lt;/bean&gt;

&lt;bean id="waffleSecurityFilterProviderCollection"

   class="waffle.servlet.spi.SecurityFilterProviderCollection"&gt;

&lt;constructor-arg&gt;

&lt;list&gt;

&lt;ref bean="negotiateSecurityFilterProvider" /&gt;  

&lt;ref bean="basicSecurityFilterProvider" /&gt;  

&lt;/list&gt;

&lt;/constructor-arg&gt;

&lt;/bean&gt;

&lt;!-- spring filter entry point --&gt;

&lt;sec:http entry-point-ref="negotiateSecurityFilterEntryPoint"&gt;

&lt;sec:intercept-url pattern="/\*\*" access="IS\_AUTHENTICATED\_FULLY" /&gt;

&lt;sec:custom-filter ref="waffleNegotiateSecurityFilter" position="BASIC\_AUTH\_FILTER" /&gt;

&lt;/sec:http&gt;

 

&lt;bean id="negotiateSecurityFilterEntryPoint"

   class="waffle.spring.NegotiateSecurityFilterEntryPoint"&gt;

&lt;property name="provider" ref="waffleSecurityFilterProviderCollection" /&gt;

&lt;/bean&gt;

&lt;!-- spring authentication provider --&gt;

&lt;sec:authentication-manager alias="authenticationProvider" /&gt;

&lt;!-- spring security filter --&gt;

&lt;bean id="waffleNegotiateSecurityFilter" class="waffle.spring.NegotiateSecurityFilter"&gt;

&lt;property name="Provider" ref="waffleSecurityFilterProviderCollection" /&gt;

&lt;property name="AllowGuestLogin" value="true" /&gt;

&lt;property name="PrincipalFormat" value="fqn" /&gt;

&lt;property name="RoleFormat" value="both" /&gt;

&lt;/bean&gt;

注意:当访问的时候最好将访问地址写成项目部署所在机器的主机名。

当浏览器发送请求时，首先经过negotiateSecurityFilterEntryPoint处理，若未经认证或认证失败，则会弹出一个页面要求输入用户名和密码，点击确定按钮后，交由waffleNegotiateSecurityFilter处理，waffleNegotiateSecurityFilter调用相应的类和方法判断用户名和密码是否正确，如果正确，在允许访问，此时可通过request.getUserPrincipal()获取登录用户的相关信息。