代码之家  ›  专栏  ›  技术社区  ›  Ben

使用自定义实现重写AuthorizationEndpoint

  •  0
  • Ben  · 技术社区  · 6 年前

    我想覆盖spring的默认值 AuthorizationEndpoint /oauth/authorize

    @RestController
    @RequestMapping("oauth/authorize")
    public class AuthorizationController {
    
        @RequestMapping
        public void authorize(@RequestParam Map<String, String> parameters, HttpServletResponse response) throws Exception {
         // todo   
        }
    
    }
    

    但是它没有映射为 映射到 默认情况下。如何删除标准实现?

    奖金

    我想提供我自己的实现的原因是因为我的rest api是无状态的,不提供会话和/或web接口,独立的angular app为我做了这件事,并授权使用passwrd grant to server。所以我想做的是将用户重定向到我的角度应用程序的approval页面并实现一个自定义 user_oauth_approval 批准 客户端调用的终结点。我不确定是否可以用spring设置,即使可以,自定义实现也可能不那么麻烦。我想听听你的见解

    0 回复  |  直到 6 年前
        1
  •  0
  •   Ethan Nguyen    5 年前

    在新控制器中插入customAuthenticationManager。

    @Autowired
    private AuthenticationManager authenticationManager;
    
    @RequestMapping(value = "${jwt.route.authentication.path}", method = RequestMethod.POST)
        public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
                throws AuthenticationException {
    
            Authentication customAuthentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                    authenticationRequest.getUsername(), authenticationRequest.getPassword()));
            SecurityContextHolder.getContext().setAuthentication(authentication);
    
            return ResponseEntity
                    .ok(new JwtAuthenticationResponse(customAuthentication.getToken(), customAuthentication.isActive()));
    
        }
    

    然后覆盖默认的Spring AuthenticationManager+AuthenticationProvider

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private CustomAuthenticationProvider customAuthenticationProvider;
    
        @Autowired
        public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) {
            authenticationManagerBuilder.authenticationProvider(customAuthenticationProvider);
        }
    
        @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    
    
    import org.springframework.security.authentication.AuthenticationProvider;
    
    @Component("customAuthenticationProvider")
    public class CustomAuthenticationProvider implements AuthenticationProvider {