Security作用描述源自:
https://www.cnblogs.com/jaylon/p/4905769.html
实操:搭配环境
一.首先需要创建IDEA创建一个SpringBoot项目,需要选择上Security
二.创建完毕后测试一下是否可以运行
(创建完项目后会有一个类,在该类中创建一个URL路径并测试是否运行)
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})//去掉数据库等信息的依赖
@RestController//@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
@EnableAutoConfiguration//SpringBoot程序入口
public class QxglxtApplication {
public static void main(String[] args) {
SpringApplication.run(QxglxtApplication.class, args);
}
//设置一个URL路径"/",输入"/"后提示"hello spring boot"页面
@RequestMapping("/")
public String home(){
return "hello spring boot";
}
}
测试结果:
实操:验证
一.自行创建一个SpringSecurityConfig类,在里面进行地址验证
要求:忽略掉项目中JS或则CSS等文件(不进行拦截),默认"/"首页,logout注销,formlogin登录不拦截(可以随意访问),当用户输入其他的网址url地址会直接进行拦截
@Configuration//等同于在Spring创建bean对象
@EnableWebSecurity//Security配置注解
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
//忽略掉JS或则CSS等文件(不进行拦截)
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**","/css/**","/images/**");
}
//默认/首页,logout注销,formlogin登录不拦截,其他的网址url直接拦截
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout()
.and()
.formLogin();
}
}
测试结果:
乱输入地址会出现这个界面,而"/"就会出现指定页面