Skip to content
TenSeventeen edited this page Jun 3, 2018 · 2 revisions

6月1日----Spring Security阅读笔记

  1. Java Configuration

    Spring Security Java Configuration 支持users去使用简单的Spring Security配置文件且不使用任何xml。
    

5.1 Hello Web Security Java Configuration

5.1.2 AbstractSecurityWebApplicationInitializer without Existing Spring

    如果你没有使用Spring or SpringMVC,那么你需要通过WebSecurityConfig来确保configuration被连接。

    认证注册spring安全过滤链为你程序里的每一个url。

    添加一个上下文监听器来装载WebSecurityConfig。

5.2 HttpSecurity

protected void configure(HttpSecurity http) throws Exception {
	http
		.authorizeRequests()
			.anyRequest().authenticated()
			.and()
		.formLogin()
			.and()
		.httpBasic();
}
  • 确保对我们应用程序的任何请求都要求用户进行身份验证
  • 允许用户使用基于表单的登录进行身份验证
  • 允许用户使用HTTP基本认证进行认证

5.3 Java Configuration and Form Login

protected void configure(HttpSecurity http) throws Exception {
	http
		.authorizeRequests()
			.anyRequest().authenticated()
			.and()
		.formLogin()
			.loginPage("/login") #1
			.permitAll();        #2
}
  1. 指定登录页面的位置。
  2. 授权所有用户(未经身份验证的用户)。

5.4 Authorize Requests

protected void configure(HttpSecurity http) throws Exception {
	http
		.authorizeRequests()        #1                                                        
			.antMatchers("/resources/**", "/signup", "/about").permitAll()#2               
			.antMatchers("/admin/**").hasRole("ADMIN")   #3                                  
			.antMatchers("/db/**").access("hasRole('ADMIN')  and      hasRole('DBA')")             #4
			.anyRequest().authenticated()            #5                                       
			.and()
		// ...
		.formLogin();
}
  1. http.autorizeRequests()方法有许多children。
  2. 如果URL以“/ resources /”开头,等于“/ signup”或等于“/ about”,则任何用户都可以访问请求。
  3. 任何以"/admin"开头的网址都将限制为具有"ROLE_ADMIN"角色的用户,但是使用了hasRole()就可以省略前缀ROLE。
  4. "/db"类似,同时具有admin和dba两种角色才能访问。
  5. 任何尚未匹配的URL只需要用户进行身份验证

5.5 Handling Logouts

protected void configure(HttpSecurity http) throws Exception {
	http
		.logout()                          #1                                      
			.logoutUrl("/my/logout")        #2                                           
			.logoutSuccessUrl("/my/index")      #3                                      
			.logoutSuccessHandler(logoutSuccessHandler)   #4                            
			.invalidateHttpSession(true)               #5                             
			.addLogoutHandler(logoutHandler)             #6                            
			.deleteCookies(cookieNamesToClear)            #7                            
			.and()
		...
}
  1. 提供注销支持,会自动应用WebSecurityConfigurerAdapter 。
  2. 触发注销的URL(默认为/logout)。如果启用CSRF保护(默认),则该请求也必须是POST。
  3. 注销后重定向到的URL。默认是/login?logout。
  4. 如果你指定了或使用了LogoutSuccessHandler, logoutSuccessUrl就会被忽略。
  5. 指定HttpSession在注销时是否使无效。默认是true。在SecurityContextLogoutHandler 下提供配置。
  6. 添加一个LogoutHandler。SecurityContextLogoutHandler 作为最后的LogoutHandler 通过默认被添加。
  7. 允许指定注销成功时删除的cookie的名称。这是CookieClearingLogoutHandler明确添加的一条捷径。

5.5.1 LogoutHandler

    LogoutHandler 实现表示能够参与注销处理的类。 

    LogoutHandlerFluent API 不是直接提供实现,而是提供快捷方式。提供LogoutHandler下的相应实现。 

5.5.2 LogoutSuccessHandler

    该LogoutSuccessHandler被成功注销后调用LogoutFilter,来处理如重定向或转发到相应的目的地。界面几乎与该界面相同,LogoutHandler但可能引发异常。 

    不需要SimpleUrlLogoutSuccessHandler直接指定。API提供了一个快捷方式,通过设置logoutSuccessUrl()。SimpleUrlLogoutSuccessHandler提供的URL将在注销发生后重定向到。默认是/login?logout。 

Clone this wiki locally