Java Spring Security OAuth2的Web和移动客户端

Java Spring Security OAuth2的Web和移动客户端,java,spring,oauth,spring-security,spring-security-oauth2,Java,Spring,Oauth,Spring Security,Spring Security Oauth2,我正试图了解OAuth2和SpringSecurityOAuth,尤其是OAuth提供者服务。我正在尝试实现以下内容: OAuth提供者 资源服务器(应使用OAuth提供程序(1)保护的RESTful Web服务) Web客户端(使用Spring安全性保护的Web客户端应用程序,但应使用OAuth提供程序(1)对用户进行身份验证) 本机移动客户端(Android和iOS)也应该使用OAuth提供程序(1)进行身份验证 所有这些模块都是相互独立的,即在不同的项目中分开,并将托管在不同的域中,例如(

我正试图了解OAuth2和SpringSecurityOAuth,尤其是OAuth提供者服务。我正在尝试实现以下内容:

  • OAuth提供者
  • 资源服务器(应使用OAuth提供程序(1)保护的RESTful Web服务)
  • Web客户端(使用Spring安全性保护的Web客户端应用程序,但应使用OAuth提供程序(1)对用户进行身份验证)
  • 本机移动客户端(Android和iOS)也应该使用OAuth提供程序(1)进行身份验证
  • 所有这些模块都是相互独立的,即在不同的项目中分开,并将托管在不同的域中,例如(1)、(2)、(3)

    我的两个问题是:

    A.如何实现Web客户端项目,以便当用户登录到受保护的页面或单击登录按钮时,重定向到OAuth提供程序url,登录,并在Web客户端上使用所有用户角色进行身份验证,同时还需要知道使用了哪个客户端。
    @EnableResourceServer
    (与资源服务器的实现方式相同;请参见下面的代码)在此项目中获取用户详细信息?我是否必须管理访问令牌并始终将其包含在对资源服务器的调用中,或者可以自动完成

    B.在我将要开发的移动应用程序上实现安全性的最佳方法是什么。我是否应该使用password grand进行此身份验证,因为这些应用程序将由我构建,我将在本地屏幕上显示用户名和密码,然后通过SSL将其作为基本身份验证发送到服务器?是否可以提供任何示例n查看与Spring Security OAuth的对话,并返回用户详细信息

    下面是我对OAuth项目(1)和资源项目(2)的实现:

    1.OAuth提供者 OAuth2服务器配置(大部分代码取自)

    Web安全配置

    @Configuration
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Autowired
        private CustomUserDetailsService customUserDetailsService;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.csrf().disable(); // TODO. Enable this!!!
    
            http.authorizeRequests()
                    .and()
                    .formLogin()
    //                .loginPage("/login") // manually defining page to login
    //                .failureUrl("/login?error") // manually defining page for login error
                    .usernameParameter("email")
                    .permitAll()
    
                    .and()
                    .logout()
    //                .logoutUrl("/logout")
                    .logoutSuccessUrl("/")
                    .permitAll();
        }
    
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth
                    .userDetailsService(customUserDetailsService)
                    .passwordEncoder(new BCryptPasswordEncoder());
        }
    
        @Override
        @Bean
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    }
    
    UserDetailsService(customUserDetailsService)

    2.资源服务器(RESTful WS) 配置(大部分框架代码取自示例)

    WS-Controller:

    @RestController
    @RequestMapping(value = "/todos")
    public class TodoController {
    
        @Autowired
        private TodoRepository todoRepository;
    
        @RequestMapping(method = RequestMethod.GET)
        public List<Todo> todos() {
            return todoRepository.findAll();
        }
    
       // other methods
    }
    
    @RestController
    @请求映射(value=“/todos”)
    公共类TodoController{
    @自动连线
    私人储蓄存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款存款;
    @RequestMapping(method=RequestMethod.GET)
    公共列表待办事项(){
    返回到orepository.findAll();
    }
    //其他方法
    }
    
    如何实现Web客户端项目,以便在用户登录时 在受保护页面的中,或单击登录按钮,可以重定向 到OAuth提供程序url、登录,并在Web客户端上进行身份验证 对于所有用户角色,还需要知道哪个客户端是 使用

    您希望使用OAuth作为SSO

    选项1,使用spring云

    选项2,手动处理SSO过程:

    在web客户端中,使用授权授权将登录页面配置为OAuth服务器

    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); // TODO. Enable this!!!
        http.authorizeRequests()
        .and()
        .formLogin()
        .loginPage("http://oauth.web.com/oauth/authorize?response_type=code&client_id=webclient&redirect_uri=http://web.com") // manually defining page to login
        //.failureUrl("/login?error") // manually defining page for login error
        .usernameParameter("email")
        .permitAll()   
        .and()
        .logout()
        //.logoutUrl("/logout")
        .logoutSuccessUrl("/")
        .permitAll();
    }
    
    身份验证和授权过程完成后,您将被重定向到具有授权代码
    http://web.com/?code=jYWioI
    。您的web客户端应在oauth服务器上使用令牌访问权交换此代码。 在oauth服务器上,创建用于检索用户信息的端点

    @RestController
    public class UserRestService {
    
      @RequestMapping("/user")
      public Principal user(Principal user) {
        // you can also return User object with it's roles
        // {"details":...,"principal":{"username":"user",...},"name":"user"}
        return user;
      }
    
    }
    
    然后,web客户端可以通过向上述rest端点发送带有令牌访问权的请求来访问用户详细信息,并根据响应对用户进行身份验证

    我是否必须管理访问令牌并始终将其包含在对的调用中 资源服务器还是可以自动完成

    每个请求都必须包含令牌访问。如果您想自动执行,spring提供了Oauth 2客户端

    在移动应用程序上实现安全性的最佳方法是什么 我会开发的。我应该使用grand密码吗 身份验证,因为应用程序将由我构建,在那里我将有一个 用户名和密码将显示在本机屏幕中,然后发送到 服务器作为SSL上的基本身份验证

    由于您使用的是本机屏幕,密码授权就足够了,但您可以存储刷新令牌,这将使您能够请求令牌访问,而无需重复身份验证过程

    有什么样的样本可以让我看一下Spring的演讲吗 安全OAuth并返回用户详细信息


    请参阅上面的示例代码或查看此代码

    谢谢您的回答。我一直在解决同一类型的问题。但是我在这一部分遇到了问题“您的web客户端应该在您的oauth服务器上使用令牌访问权交换此代码。在您的oauth服务器上,创建一个用于检索用户信息的端点。”什么端点——假设我有一个像你链接的博客帖子那样的设置,我的客户端应该使用吗?我没有成功,也没有看到spring提供的具有实际端点工作流的好文档。@adeady在你的web客户端中创建服务,将代码发布到
    /oauth/token
    端点,这将为你提供令牌。oauth您获得的密钥可用于访问受保护的资源。
    @RestController
    @RequestMapping(value = "/todos")
    public class TodoController {
    
        @Autowired
        private TodoRepository todoRepository;
    
        @RequestMapping(method = RequestMethod.GET)
        public List<Todo> todos() {
            return todoRepository.findAll();
        }
    
       // other methods
    }
    
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); // TODO. Enable this!!!
        http.authorizeRequests()
        .and()
        .formLogin()
        .loginPage("http://oauth.web.com/oauth/authorize?response_type=code&client_id=webclient&redirect_uri=http://web.com") // manually defining page to login
        //.failureUrl("/login?error") // manually defining page for login error
        .usernameParameter("email")
        .permitAll()   
        .and()
        .logout()
        //.logoutUrl("/logout")
        .logoutSuccessUrl("/")
        .permitAll();
    }
    
    @RestController
    public class UserRestService {
    
      @RequestMapping("/user")
      public Principal user(Principal user) {
        // you can also return User object with it's roles
        // {"details":...,"principal":{"username":"user",...},"name":"user"}
        return user;
      }
    
    }