使用SpringBoot搭建SpringSecurity环境与验证

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();

    }
}

 

 

测试结果:
乱输入地址会出现这个界面,而"/"就会出现指定页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值