入门

作者: 笨比乔治 | 来源:发表于2020-12-08 10:36 被阅读0次

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

name默认是user
密码


image.png

Spring Security本质是一个过滤器链,有很多的过滤器
有三个重要的过滤器

1.FilterSecurityInterceptor:是一个方法级的权限过滤器,基本位于过滤器的最底部。

2.ExceptionTranslationFilter:是个异常过滤器,用来处理在认证授权过程中抛出的异常。

3.UsernamePasswordAuthenticationFilter对/login的post请求做拦截,检验表单中用户名、密码。

image.png

两个重要的接口

1.UserDetailService接口:查询数据库用户名和密码过程

*创建类继承UsernamePasswordAuthenticationFilter,重写三个方法(accept/success/unsuccess)

  • 创建类实现UserDetailService:编写查询数据过程,返回User对象,这个User对象是安全框架提供的对象。

2.PasswordEncoder 接口

用于数据密码的加密

配置类设置账户密码

/**
 * @author qiz
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123");

        auth.inMemoryAuthentication().withUser("lucy").password(encode).roles("admin");
    }

    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}

自定义实现类

/**
 * @author qiz
 */
@Configuration

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailsService).passwordEncoder(password());

    }

    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
}
/**
 * @author qiz
 */
@Service("userDetailsService")
public class UserDetailService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        return new User("mary",new BCryptPasswordEncoder().encode("123"),auths);
    }
}

连接数据库


image.png
image.png
 @Override
    protected void configure(HttpSecurity http) throws Exception{

        //配置没有权限访问跳转自定义页面
        http.exceptionHandling().accessDeniedPage("/error/404.html");

        //自定义自己编写的登录页面
            http.formLogin()
                    //登录页面设置
                .loginPage("/login.html")
                    //登录访问路径
                .loginProcessingUrl("/user/login")
                    //登录成功之后,跳转的路径
                    .defaultSuccessUrl("/user/index")

                    .and().authorizeRequests()
                    //直接访问,不需要认证
                    .antMatchers("/test/add","/user/login").permitAll()
                    //其他请求需要认证
//                    .anyRequest().authenticated()
                    //具有某个权限才能访问
//                    .antMatchers().hasAuthority()
                    //有多个权限
//                    .antMatchers().hasAnyAuthority()
                    //具有指定角色
//                    .antMatchers().hasRole()  ROLE_sale
                    .and().csrf().disable();
    }

image.png image.png

使用注解

image.png
image.png
image.png

hasAnyAuthority与hasAnyRole区别

Role前面必须加ROLE_,因为获取角色是会加ROLE_。

用户退出


image.png

记住我


image.png
image.png

相关文章

网友评论

      本文标题:入门

      本文链接:https://www.haomeiwen.com/subject/icjzbktx.html