spring security 个人 笔记 #spring#springcloud

  全文照  https://github.com/wuyouzhuguli/SpringAll   MrBird课程敲的代码

  包含了:Spring security自定义用户认证、图形验证码、记住我、短信验证码、session管理、退出登录

权限控制

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /*
         * 第一课 基本方式与表单方式配置 // http.httpBasic() //基本方式 http.formLogin() // 表单方式 .and()
         * .authorizeRequests() // 授权配置 .anyRequest() // 所有请求 .authenticated(); // 都需要认证
         */
        http.
            exceptionHandling()//权限异常处理类
                .accessDeniedHandler(authenticationAccessDeniedHandler)
                .and()
                .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)// 让验证码拦截器处于其他security过滤器前面
                .addFilterBefore(smsCodeFilter, UsernamePasswordAuthenticationFilter.class) // 添加短信验证码校验过滤器        
                .formLogin() // 表单登录
                .loginPage("/authentication/require").loginProcessingUrl("/login") // 处理表单登陆的URL
                .successHandler(authenticationSucessHandler).failureHandler(authenticationFailureHandler)
                .and()
                .rememberMe()   //会自动从数据库中
                .tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(3600)
                .userDetailsService(myUserDetailService)
                .and()
                .authorizeRequests() // 授权配置
                .antMatchers("/authentication/require", "/login.html", "/code/image", "/css/login.css","/code/sms","/signout/success").permitAll()
                .anyRequest() // 所有请求
                .authenticated() // 都需要认证
                .and()
                .sessionManagement() // 添加 Session管理器
                .invalidSessionUrl("/session/invalid") // Session失效后跳转到这个链接
                /**另外一种方式 session并发处理机制为新加入的用户被拒绝  
                 *         在实际开发中,
                 *         发现Session并发控制只对Spring Security默认的登录方式——账号密码登录有效,
                 *         而像短信验证码登录,社交账号登录并不生效
                 *  .maximumSessions(1) //最大session并发数量
                 *  .maxSessionsPreventsLogin(true) 
                .    .expiredSessionStrategy(sessionExpiredStrategy)
                 */
                .maximumSessions(1) //最大session并发数量
                .expiredSessionStrategy(sessionExpiredStrategy)
                .and()
                .and()
                .logout()
                .logoutUrl("/signout")
//                .logoutSuccessUrl("/signout/success")   //登出发成功跳转方法
                .logoutSuccessHandler(myLogOutSuccessHandler) //登出成功跳转的Handler
                .deleteCookies("JSESSIONID")
                .and().csrf().disable()
                .apply(smsAuthenticationConfig);// 关闭CSRF攻击防御
                
        /**
         * 关于antMatchers() begin
         */
        // 我们指定任何用户都可以访问多个URL的模式。
        // 任何用户都可以访问以"/resources/","/signup", 或者 "/about"开头的URL。
        // .antMatchers("/resources/**", "/signup", "/about").permitAll()

        // 以 "/admin/" 开头的URL只能让拥有 "ROLE_ADMIN"角色的用户访问。
        // 请注意我们使用 hasRole 方法,没有使用 "ROLE_" 前缀。
        // .antMatchers("/admin/**").hasRole("ADMIN")

        // 任何以"/db/" 开头的URL需要同时具有 "ROLE_ADMIN" 和 "ROLE_DBA"权限的用户才可以访问。
        // 和上面一样我们的 hasRole 方法也没有使用 "ROLE_" 前缀。
        // .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")

        // 任何以"/db/" 开头的URL只需要拥有 "ROLE_ADMIN" 和 "ROLE_DBA"其中一个权限的用户才可以访问。
        // 和上面一样我们的 hasRole 方法也没有使用 "ROLE_" 前缀。
        // .antMatchers("/db/**").hasAnyRole("ADMIN", "DBA")
        // -----------------end----------------

2.验证,从provider 到 detailService 密码使用PasswordEncoder的match方法

3.其他都在项目中看吧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值