您的位置:首页 > 编程语言 > Java开发

【Spring】官网教程阅读笔记(四):使用LDAP对用户鉴权

2015-03-31 10:55 501 查看
【前言】再接再厉,LDAP鉴权或许能用到。本文给了两个application作为参照,第一个是普通的webApplication,没有用户鉴权,第二个有用户鉴权。如果看过第一篇,可以直接跳到第二个application中学习。原文链接http://spring.io/guides/gs/authenticating-ldap/

【实现目标】你在这里将创建一个web应用,该应用使用Spring内置安全模块:基于Java的LDAP服务。你将加载LDAP服务管理一个包含一个用户集合的数据文件。

【准备工作】pom.xml 中的dependencies和第一篇一样

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>


创建一个简单的web控制器

Spring中,REST节点只是一个MVC控制器Controller。下面的控制器处理GET /目录的请求,并且返回一个简单的信息,还记得第一章的注解@RESTController和@RequestMapping么?还有@RequestParameter这里就不用了。

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

@RequestMapping("/")
public String index() {
return "Welcome to the home page!";
}
}


这一次没返回Json,而只是一个String。

整个类由注解@RESTController标注,此时Spring MVC可以通过内建扫描机制自动检测到控制器,并自动配置web页路由(指web跳转)。@RequestMapping注解标注的方法绑定了REST的action。在本例中,GET方法到/目录被绑定到index()方法来处理。这个方法返回一个String信息表明你访问到了home页。

@RESTController注解也会告知Spring MVC框架将文本直接写入HTTP应答报文当中,因为这里没有任何视图(View)。此时,当你访问/页面的时候,你会在浏览器上得到一个简短的信息,因为本文聚焦在LDAP的使用。

创建一个不安全的web应用

在着手做安全的web应用之前,为了能确认他确实安全,我们先建一个不安全的web应用作参照物。这里需要建几个key bean。先把应用建起来。

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
@SpringBootApplic注解我们已经见过3次了。SpringApplication.run()方法也有2次了,他们的功能不说了。

Build并执行JAR

和以前一样

设置Spring安全机制

设置安全机制要先增加几个dependency到build当中。按maven的pom.xml配置来说,在原来的<dependencies>中增加下面几个

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-jndi</artifactId>
<version>1.5.5</version>
</dependency>
坑爹的教程,一次不说完。反正加好之后就可以做纯java的配置类了

package hello;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();
}

@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource().ldif("classpath:test-server.ldif");
}
}
}


注解@EnableWebSecurity为需要安全验证的bean打开Spring 安全验证功能。你还需要一个LDAP服务,Spring内置LDAP模块包含了这样一个用纯Java实现的服务,我们在这里用到了它(ldapAuthentication())。ldapAuthentication()方法把form表单中的username置为{0},这样框架就在LDAP服务中按
uid={0},ou=people,dc=springframework,dc=org

搜索。

设置用户数据

LDAP服务可以使用LDIF(LDAP数据交换格式)文件来交换用户数据。WebSecurityConfig类中的ldif()方法可以导入一个LDIF数据文件。这样可以很方便的加载验证数据。

src/main/resources/test-server.ldif


(为了节省空间,该文件略过,可以查看原文)


LDIF对一个产品级系统来说并非标准配置,然而作为测试或者样例还是不错的选择。

应用类还是一样的代码

Build并执行JAR

Build步骤并没有变化

执行的时候, 会重定向到Login页面,然后输入username和password,比如ben和benpassword,最后得到应答welcom to the home page!

【小结】

原文中说的并不详细,实际上WebSecurityConfig类的实现有好多东西需要解释:

注解@Configuration 可以参照文档http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-basic-concepts
。一个带@Configuration注解的类表明:他的主要目的是用来定义bean。进一步,@Configuration注解类允许内置bean依赖策略,这些策略由同类中调用其他@bean方法定义。最简单的@Configuration类看起来可能是这样:

@Configuration
public class AppConfig {

@Bean
public MyService myService() {
return new MyServiceImpl();
}

}
他的作用就是绑定接口MyService的实现类MyServiceImpl。就好比配置文件中这么写

<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>


不过本文中的@Configuration里面表明上并没有标注一个@Bean是怎么回事?这是因为@Bean在抽象类
WebSecurityConfigurerAdapter当中,一个是接口UserDetailsService,一个是接口AuthenticationManager


注解@EnableWebMvcSecurity 。给@Configuration类添加这个注解,使得该类以Spring安全模式集成在Spring MVC当中。

抽象类WebSecurityConfigurerAdapter提供WebSecurityCOnfigure功能。
在本例中覆盖了方法 void configure(HttpSecurity http)。之后调用的几个方法,他们的含义分别是:authorizeRequests()对HttpServletRequest做访问限制;antMatchers()是一个HTTPMethod匹配器,"\**"或者"**"表示匹配任意请求格式,而/aaa/**将匹配/aaa或者/aaa/,在本例当中匹配了/css或/css/的URL;permitAll()运行任何用户访问该URL;anyRequest()任意HTTP请求类型;fullyAuthenticated()对有权限用户开放,并且对鉴权信息是无记忆的;and()且;formLogin()警察叔叔,就是他生成了Login的form表单!如果想自定义Login,需要使
FormLoginCOnfigurer.loginPage(String)。

内部类AuthenticationConfiguration中的方法init()也说一下。ldapAuthentication()方法增加ldap功能;userDnPatterns()从LDIF文件中直接定位参数;groupSearchBase()按组搜索;contextSource()使用简单的内置LDAP服务;ldif()用指定的ldif文件加载到内置LDAP服务中。

【疑问】如何生成LDIF文件呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: