package com.itheima.config;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
//指定认证对象的来源
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}
//SpringSecurity配置信息
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login.jsp", "failer.jsp", "/css/**", "/img/**", "/plugins/**").permitAll()//定义了默认允许访问的资源(不需要认证)
.antMatchers("/product").hasAnyRole("USER")// 任何以“/ product /”开头的URL都将仅限于具有“USER”角色的用户
.anyRequest().authenticated()// 任何尚未匹配的URL只需要对用户进行身份验证
.and()//
.formLogin() //
.loginPage("/login.jsp")// 设置登录页面是/login.jsp
.loginProcessingUrl("/login")//
.successForwardUrl("/index.jsp")// 登录成功的跳转
.failureForwardUrl("/failer.jsp")// 登录失败的跳转
.and()
.logout()
.logoutSuccessUrl("/logout") // 自定义一个LogoutSuccessHandler。如果指定了此参数,则忽略logoutSuccessUrl
.invalidateHttpSession(true)//指定在注销时是否使HttpSession无效。默认为true
.logoutSuccessUrl("/login.jsp")// 退出登录后的重定向
.and()
.csrf()// 启动csrf防攻击策略
.disable();
}
}
更多配置,参考 https://blog.csdn.net/weixin_30767835/article/details/96154077
Spring Security常用过滤器介绍
1. org.springframework.security.web.context.SecurityContextPersistenceFilter
首当其冲的一个过滤器,作用之重要,自不必多言。
SecurityContextPersistenceFilter
主要是使用
SecurityContextRepository
在
session
中保存或更新一个
SecurityContext
,并将
SecurityContext
给以后的过滤器使用,来为后续
fifilter
建立所需的上下文。
SecurityContext
中存储了当前用户的认证以及权限信息。
2. org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter
此过滤器用于集成
SecurityContext
到
Spring
异步执行机制中的
WebAsyncManager
3. org.springframework.security.web.header.HeaderWriterFilter
向请求的
Header
中添加相应的信息
,
可在
http
标签内部使用
security:headers
来控制
4. org.springframework.security.web.csrf.CsrfFilter
csrf
又称跨域请求伪造,
SpringSecurity
会对所有
post
请求验证是否包含系统生成的
csrf
的
token
信息,
如果不包含,则报错。起到防止
csrf
攻击的效果。
5. org.springframework.security.web.authentication.logout.LogoutFilter
匹配
URL
为
/logout
的请求,实现用户退出
,
清除认证信息。
6. org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
认证操作全靠这个过滤器,默认匹配
URL
为
/login
且必须为
POST
请求。
7. org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
如果没有在配置文件中指定认证页面,则由该过滤器生成一个默认认证页面。
8. org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
由此过滤器可以生产一个默认的退出登录页面
9. org.springframework.security.web.authentication.www.BasicAuthenticationFilter
此过滤器会自动解析
HTTP
请求中头部名字为
Authentication
,且以
Basic
开头的头信息。
10. org.springframework.security.web.savedrequest.RequestCacheAwareFilter
通过
HttpSessionRequestCache
内部维护了一个
RequestCache
,用于缓存
HttpServletRequest
11. org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
针对
ServletRequest
进行了一次包装,使得
request
具有更加丰富的
API
12. org.springframework.security.web.authentication.AnonymousAuthenticationFilter
当
SecurityContextHolder
中认证信息为空
,
则会创建一个匿名用户存入到
SecurityContextHolder
中。
spring security
为了兼容未登录的访问,也走了一套认证流程,只不过是一个匿名的身份。
13. org.springframework.security.web.session.SessionManagementFilter
SecurityContextRepository
限制同一用户开启多个会话的数量
14. org.springframework.security.web.access.ExceptionTranslationFilter
异常转换过滤器位于整个
springSecurityFilterChain
的后方,用来转换整个链路中出现的异常
15. org.springframework.security.web.access.intercept.FilterSecurityInterceptor
获取所配置资源访问的授权信息,根据
SecurityContextHolder
中存储的用户信息来决定其是否有权
限。