【前言】再接再厉,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>
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!"; } }
整个类由注解@RESTController标注,此时Spring MVC可以通过内建扫描机制自动检测到控制器,并自动配置web页路由(指web跳转)。@RequestMapping注解标注的方法绑定了REST的action。在本例中,GET方法到/目录被绑定到index()方法来处理。这个方法返回一个String信息表明你访问到了home页。
@RESTController注解也会告知Spring MVC框架将文本直接写入HTTP应答报文当中,因为这里没有任何视图(View)。此时,当你访问/页面的时候,你会在浏览器上得到一个简短的信息,因为本文聚焦在LDAP的使用。
在着手做安全的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次了,他们的功能不说了。
和以前一样
设置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"); } } }
uid={0},ou=people,dc=springframework,dc=org
搜索。
LDAP服务可以使用LDIF(LDAP数据交换格式)文件来交换用户数据。WebSecurityConfig类中的ldif()方法可以导入一个LDIF数据文件。这样可以很方便的加载验证数据。
src/main/resources/test-server.ldif
(为了节省空间,该文件略过,可以查看原文)
LDIF对一个产品级系统来说并非标准配置,然而作为测试或者样例还是不错的选择。
应用类还是一样的代码
执行的时候, 会重定向到Login页面,然后输入username和password,比如ben和benpassword,最后得到应答welcom to the home page!
【小结】
原文中说的并不详细,实际上WebSecurityConfig类的实现有好多东西需要解释:
@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
FormLoginCOnfigurer.loginPage(String)。
【疑问】如何生成LDIF文件呢?