1.为什么加入依赖密码和用户生成在哪里?
    在 Spring Security 中,默认的登录页面和登录接口,都是 /login ,只不过一个是 get 请求(登录页面),另一个是 post 请求(登录接口)。和用户相关的自动化配置类在 UserDetailsServiceAutoConfiguration

    2.加密
    Spring Security 提供了多种密码加密方案,官方推荐使用 BCryptPasswordEncoder,BCryptPasswordEncoder 就自带了盐, BCryptPasswordEncoder 就是 PasswordEncoder 接口的实现类。

    3.定义内存用户
    重写configure方法,利用inMemoryAuthentication()

    4放行地址
    重写configure(WebSecurity web),web.ignoring() 用来配置忽略掉的 URL 地址,一般对于静态文件

    5.数据源
    Spring Security 支持多种不同的数据源,这些不同的数据源最终都将被封装成 UserDetailsService 的实例

    6.自定义认证逻辑(自定义过滤器)
    用户登录的用户名/密码是在 UsernamePasswordAuthenticationFilter 类中处理,自定义一个过滤器代替 UsernamePasswordAuthenticationFilter,

    7.自定义认证
    AuthenticationProvider 定义了 Spring Security 中的验证逻辑,AuthenticationProvider 中就两个方法:

    • authenticate 方法用来做验证,就是验证用户身份。
    • supports 则用来判断当前的 AuthenticationProvider 是否支持对应的 Authentication。

    可以在任何地方注入 Authentication 进而获取到当前登录用户信息,Authentication 本身是一个接口,它实际上对 java.security.Principal 做的进一步封装,
    public interface Authentication extends Principal, Serializable {
    Collection<? extends GrantedAuthority> getAuthorities();
    Object getCredentials();
    Object getDetails();
    Object getPrincipal();
    boolean isAuthenticated();
    void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
    }
    getAuthorities 方法用来获取用户的权限。
    getCredentials 方法用来获取用户凭证,一般来说就是密码。
    getDetails 方法用来获取用户携带的详细信息,可能是当前请求之类的东西。
    getPrincipal 方法用来获取当前用户,可能是一个用户名,也可能是一个用户对象。
    isAuthenticated 当前用户是否认证成功。
    Authentication 作为一个接口,它定义了用户,或者说 Principal 的一些基本行为,它有很多实现类,最常用的就是 UsernamePasswordAuthenticationToken 了,而每一个 Authentication 都有适合它的 AuthenticationProvider 去处理校验。例如处理 UsernamePasswordAuthenticationToken 的 AuthenticationProvider 是 DaoAuthenticationProvider。
    自定义一个 MyAuthenticationProvider 继承自 DaoAuthenticationProvider,并重写 additionalAuthenticationChecks 方法:MyAuthenticationProvider 定义好之后,接下来主要是如何让 MyAuthenticationProvider 代替 DaoAuthenticationProvider。所有的 AuthenticationProvider 都是放在 ProviderManager 中统一管理的,所以接下来就要自己提供 ProviderManager,然后注入自定义的 MyAuthenticationProvider,这一切操作都在 SecurityConfig 中完成:

    @Bean
    MyAuthenticationProvider myAuthenticationProvider() {
    MyAuthenticationProvider myAuthenticationProvider = new MyAuthenticationProvider();
    myAuthenticationProvider.setPasswordEncoder(passwordEncoder());
    myAuthenticationProvider.setUserDetailsService(userDetailsService());
    return myAuthenticationProvider;
    }

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
    ProviderManager manager = new ProviderManager(Arrays.asList(myAuthenticationProvider()));
    return manager;
    }

    1. 授权

    在 Spring Security 的 configure(HttpSecurity http) 方法中,
    http.authorizeRequests()
    .antMatchers(“/admin/“).hasRole(“admin”)
    .antMatchers(“/user/
    “).hasRole(“user”)
    .anyRequest().authenticated()
    .and()
    …
    …
    这里的匹配规则采用了 Ant 风格的路径匹配符

    通配符 含义
    ** 匹配多层路径
    * 匹配一层路径
    ? 匹配任意单个字符

    如果请求路径满足 /admin/ 格式,则用户需要具备 admin 角色。
    如果请求路径满足 /user/
    格式,则用户需要具备 user 角色。
    剩余的其他格式的请求路径,只需要认证(登录)后就可以访问。