spring security 学习

112 阅读3分钟

建立maven项目后,引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

随机访问一个项目链接,会跳转到springSecurity自带登录页面

image.png

设定用户名和密码

配置写入

spring.security.user.name=admin
spring.security.user.password=123456

配置security

通过WebSecurityConfigurerAdapter来配置

configure(WebSecurity web)

configure(WebSecurity)用于 影响全局安全性的
配置设置(忽略资源,设置调试模式,通过实现自定义防火墙定义拒绝请求)。例如,以下方法将导致 以身份验证为 开头的所有请求/resources/都被
忽略 。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/js/**");
}

configure(HttpSecurity http)

configure(HttpSecurity)允许根据选择匹配在 资源级别 配置基于Web的安全性-
例如,以下示例将以URL开头的URL限制为/admin/具有 ADMIN角色的 用户,并声明需要 成功进行身份验证的 所有其他URL 

protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
            // 校验成功后跳转地址
            .successForwardUrl("/home")
            // 校验失败后跳转地址
            .failureForwardUrl("/toError")
            // 定义首页地址
            .loginPage("/login.html")
            // 登录请求地址
            .loginProcessingUrl("/login")
            // 用户名参数名
            .usernameParameter("name")
            // 密码参数名
            .passwordParameter("pass")
            // 链接,直接从http.开始
            .and()
            // 定义antMatchers内的请求不用校验
    .authorizeRequests().antMatchers("/**.html","/home","/error","/login","logout").permitAll()
            // 定义所有请求需求校验
            .anyRequest().authenticated().and().
            // 退出请求地址
    logout().logoutUrl("/logout").
            // 退出成功后的跳转地址
             logoutSuccessUrl("/login.html");
             //关闭csrf防护
    http.csrf().disable();
}

configure(AuthenticationManagerBuilder auth)

用户权限校验

public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("admin")
            .password(new BCryptPasswordEncoder().encode("123456"))
            .and()
            .withUser("zhl")
            .password(new BCryptPasswordEncoder().encode("123456"));
}

访问控制url匹配

anyRequest() 表示匹配所有的请求。 antMatcher() 参数是不定向参数,每个参数是一个 ant 表达式,用于匹配 URL规则。 规则如下:

? : 匹配一个字符  
* :匹配 0 个或多个字符  
** :匹配 0 个或多个目录

regexMatchers() 使用正则表达式进行匹配。和 antMatchers() 主要的区别就是参数, antMatchers() 参数是 ant 表达式, regexMatchers() 参数是正则表达式。

内置访问控制方法

  • permitAll()表示所匹配的 URL 任何人都允许访问。
  • authenticated()表示所匹配的 URL 都需要被认证才能访问。
  • anonymous()表示可以匿名访问匹配的URL。和permitAll()效果类似,只是设置为
  • anonymous()的 url 会执行 fifilter 链中
  • denyAll()表示所匹配的 URL 都不允许被访问。
  • rememberMe() 被“remember me”的用户允许访问
  • fullyAuthenticated() 如果用户不是被 remember me 的,才可以访问

角色权限判断

  • hasAuthority(String) 判断用户是否具有特定的权限,用户的权限是在自定义登录逻辑中创建 User 对象时指定的。
  • hasAnyAuthority(String ...) 如果用户具备给定权限中某一个,就允许访问。
  • hasRole(String) 如果用户具备给定角色就允许访问。否则出现 403
  • 在给用户赋予角色时角色需要以: ROLE_开头 ,后面添加角色名称。例如:ROLE_abc 其中abc 是角色名,ROLE_是固定的字符开头。
  • 使用 hasRole()时参数也只写 abc 即可。否则启动报错
  • hasAnyRole(String ...) 如果用户具备给定角色的任意一个,就允许被访问
  • hasIpAddress(String) 如果请求是指定的 IP 就运行访问。