SpringSecurity(安全框架)入门配置

1. SpringSecurity简介

官网:Spring Security

依赖于Maven环境

提供“认证”和“授权”保护

2. 开发环境

2.1 新建Springboot项目

TODO

2.3 作成Controller访问类

TODO

3. 修改配置

3.1 POM引入SpringSecurity依赖

可以通过这个网址查找Springboot的依赖https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#using-boot-starter

<dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-security</artifactId>

</dependency>

注释:引入后注意刷新maven

3.2 新建config

1.在config包下,新建xxxConfig类

2.继承 WebSecurityConfigurerAdapter

3.追加类注解 @EnableWebSecurity

4.重写configure(HttpScurity http)方法(授权)

    http.authorizeRequests() //认证请求

        .antMatchers("/").permitAll() // 请求路径,permitAll所有人可以

        .antMatchers("/v1").hasRole("vip1") // vip1权限可以访问 v1请求

    http.formLogin(); // 没有权限跳转login请求 (默认:Login,可指定url)

    http.logout(); // 注销

    http.logout().deleteCookies("remove").invalidateHttpSession(false); //注销,清除cookie和session

    http.logout().logoutUrl("/index"); // 注销成功跳转请求

    http.csrf().disable(); // 防止网站攻击(关闭跨站请求伪造)

5.重写configure(AuthenticationManagerBuilder auth)方法(认证)

    auth.jdbcAuthentication // JDBC数据库认证(两种认证二选一)

    auth.inMemoryAuthentication // 内存认证(两种认证二选一)

        .withUser("user1").password("123456").roles("vip1")

        .and()

        .withUser("user2").password("123456").roles("vip2")

    注释:SpringSecurity2.1.0及以上版本密码需要加密

    auth.inMemoryAuthentication.passwordEncoder(new BCryptPasswordEncoder())

        .withUser("user1").password(new BCryptPasswordEncoder("123456")).roles("vip1")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 是一个强大且灵活的认证和授权框架,用于保护 Spring Boot 应用程序的安全性。下面是一个简单的入门指南,帮助你开始使用 Spring Security: 1. 添加 Spring Security 依赖:在你的项目的 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建一个 Spring Security 配置类:创建一个类并注解为 `@Configuration`,这个类将用于配置 Spring Security。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public").permitAll() // 允许访问公共资源 .anyRequest().authenticated() // 其他请求需要认证 .and() .formLogin() // 启用表单登录 .and() .logout() // 启用注销功能 .and() .csrf().disable(); // 禁用 CSRF(跨站请求伪造)保护 } } ``` 在上述示例中,我们配置了一些基本的安全规则。可以自定义更多的规则来满足你的需求,比如配置自定义登录页面、添加用户角色等。 3. 添加用户认证:在上述配置类中,可以添加一个内存用户存储来进行简单的用户认证。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("admin") .password("{noop}password") // 使用 {noop} 前缀表示不加密密码 .roles("ADMIN"); } } ``` 在上述示例中,我们创建了一个用户名为 "admin"、密码为 "password"、角色为 "ADMIN" 的用户。 这只是 Spring Security入门指南,你可以进一步学习如何使用数据库存储用户信息、配置角色权限等。希望这些信息对你有所帮助!如果你有更多问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值