1.配置类注解:
@Configuration注解的的作用: @Configuration用于定义配置类,可替换xml配置文件.
@Configuration等价于xml文件中beans标签
@Bean会把对象交给SpringIOC容器管理,Spring可以通过@Autowired获取到该对象.
@Bean等价于bean标签
@ComponentScan 的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中.
@ComponentScan等价于xml文件中
@ConfigurationProperties注解通过与其他注解配合使用,能够实现Bean的按需配置。
ConfigurationProperties有一个prefix属性,通过指定的前缀,绑定配置文件中的配置,该注解可以放
在类上,也可以放在方法上将该注解作用于方法上时,如果想要有效的绑定配置,那么该方法需要有
@Bean注解且所属Class需要有@Configuration注解。
@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效。
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中
是获取不到properties 配置文件转化的bean。相当于 @EnableConfigurationProperties 把使用
@ConfigurationProperties 的类进行了一次注入。
2.拦截器配置类:
@Configuration
publicclassInterceptorimplementsWebMvcConfigurer {
@Autowired
MyInter1myInter1;
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry) {
registry.addInterceptor(myInter1)
.addPathPatterns(“/“)
.excludePathPatterns(“/login.html”,”/regist.html”,
“/layui/
“,”/user/login”,”/user/regist”,”/kaptcha/**”);
}
}


3.过滤器配置类:
4.启动器配置类:
5.SecurityConfig配置类:
@Configuration
publicclassFilterConfig {
@Autowired
CapCharFiltercapCharFilter;
@Bean
publicFilterRegistrationBean
capCharFilterFilterRegistrationBean(){
finalFilterRegistrationBeanregistrationBean=new
FilterRegistrationBean<>();
registrationBean.setFilter(capCharFilter);
registrationBean.addUrlPatterns(“/*”);
registrationBean.setOrder(-100);
returnregistrationBean;
}
}
@Configuration
@EnableConfigurationProperties(QfsyProperties.class)
publicclassQfsyConfig {
@Bean
publicQfsyServiceqfsyProperties(){
QfsyServiceImplqfsyService=newQfsyServiceImpl();
returnqfsyService;
}
}
@Data
@ConfigurationProperties(prefix=”qfys”)
publicclassQfsyProperties {
Stringusername;
Stringpassword;
Stringurl;
}
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
MyUserDetailsService myUserDetailsService;

@Autowired
PasswordEncoder passwordEncoder;
@Autowired
DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
// 使用创建配置类实现一些设置
// 创建一个加密器
// final BCryptPasswordEncoder bCryptPasswordEncoder = new
BCryptPasswordEncoder();
// 得到密码的加密密文字符串
// final String encode = bCryptPasswordEncoder.encode(“123456”);
// 设置内存保留用户名和密码
//
auth.inMemoryAuthentication().withUser(“qfadmin”).password(encode).roles(“admin”
);

auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置登录页面
http.formLogin()
.loginPage(“/login.html”) // 指定使用的登录页面
.loginProcessingUrl(“/user/login”) // 指定登录请求发送的路径
// .defaultSuccessUrl(“/index.html”)
.successHandler((request, response, authentication) -> {
response.setContentType(“application/json;charset=UTF-8”);
response.getWriter().write(“{\”statusCode\”:\”1001\”}”);
})
// .failureHandler()
.permitAll()
;
// 配置多长时间内可以不需要用户名和密码进行登录
http.rememberMe()
.userDetailsService(myUserDetailsService) // 设置自定的根据用户名加
载用户信息的bean
.tokenRepository(persistentTokenRepository()) // 设置持久化方案
.tokenValiditySeconds(60602414); // 指定免登录有效期,两周
// 返回页面的配置
// http.formLogin()
// .loginPage(“/form.html”)
// .loginProcessingUrl(“/login/login”)
// .defaultSuccessUrl(“/index.html”)// .permitAll()
// ;
// 配置哪些路径可以放行,默认的其他路径都需要登录才可以访问
http.authorizeRequests()
.antMatchers(“/test/test1”).permitAll() // 放行指定路径
/test/test1
.antMatchers(“/layui/*
“).permitAll()
.antMatchers(“/test/test2”).hasAuthority(“user”) // 访
问/test/test2需要当前用户具有admin权限
.antMatchers(“/test/test1”).hasAnyAuthority(“admin,user,test”)
.antMatchers(“/test/test3”).hasRole(“student”)
.antMatchers(“/test/test4”).hasAnyRole(“student,teacher”)
.anyRequest().authenticated(); // 默认拦截所有路径的请求,必须登录后才
可以访问
// http.csrf().disable(); // 禁用 csrf 防护

http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// 注销相关配置

http.logout().logoutUrl(“/logoff”).logoutSuccessUrl(“/logout.html”).permitAll();
// 添加权限异常页面
http.exceptionHandling().accessDeniedPage(“/error.html”);
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
// 配置持久化 token 到数据库中
public PersistentTokenRepository persistentTokenRepository(){
final JdbcTokenRepositoryImpl jdbcTokenRepository = new
JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
// jdbcTokenRepository.setCreateTableOnStartup(true); // 是否自动创建存放
cookie信息的数据库表
return jdbcTokenRepository;
}
}