Spring Security的高级认识,在上一篇我们已经初步的了解到了Spring Secuity

2 自定义登录成功处理

默认情况下,登录成功后,Spring Security 会跳转到之前引发登录的那个请求上。

AuthenticationSuccessHandler

@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        System.out.println("登录成功");

        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(objectMapper.writeValueAsString(authentication));
    }
}
@Configuration
@EnableWebSecurity
@ComponentScan("com.sxnd.authentication")
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("myAuthenticationSuccessHandler")
    private AuthenticationSuccessHandler successHandler;

    @Bean("objectMapper")
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

  ...

    @Override
    protected void configure(HttpSecurity http) throws Exception {
          http
                  .formLogin()
                  .loginPage("/sign-in.html")
                  .loginProcessingUrl("/login")
          .successHandler(successHandler)
              .and()
          ...

    }
}

短信验证码登录

生成 - 存session - 发送

Spring Social 开发第三方登录

OAuth

用户名密码授权的问题:

  • 应用可以访问用户在微信上的所有数据
  • 用户只有修改密码,才能收回授权(但又会引起其他问题)
  • 密码泄露的可能性大大增加

用户给 APP 的是token,而不再是用户名密码。App 通过携带token去访问微信自拍照片。token中可以设置有效期。

  • Provider,服务提供商(例如,微信)
    • Authorization Server(认证,发令牌)
    • Resource Server(保存资源)
  • Resource,资源(例如,微信自拍照片)
  • Resource Owner,资源所有者(例如,微信用户。注意,自拍照片的所有者并非微信)
  • Client,第三方应用
  1. 用户访问 Client
  2. Client 向该用户请求/申请授权
  3. 用户同意 Client 授权申请
  4. Client 向 Provider 的 Authorization Server 申请令牌
  5. Provider 向 Client 发放令牌
  6. Client 向 Provider 的 Resource Server 申请获取资源
  7. Provider 向 Client 开放资源
  8. Client 持续携带令牌访问 Provider 上的资源。

OAuth 协议中的四种授权模式(涉及上述第 2 步):

  • 授权码模式(功能最完整,流程最严密,应用最广泛)
  • 密码模式
  • 客户端模式
  • 简化模式

授权码模式:

授权码的模式的特点在于,“用户同意 Client 授权申请” 的过程是 用户 - Client - Provider 三方之间的交流:

  • 用户访问 Client 之后,Client 会将用户导向 Provider,Provider 向用户询问是否对 Client 授权
  • 用户同意授权后,Provider 向 Client 返回一个授权码。
  • Client 再用授权码申请 Token
  • Provider 确认授权码后发放 Token
  • Client 携带 Token 访问用户在 Provider 上的资源

第三方登录原理

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值