自定义AuthenticationProvider不被调用

马丁·梅尔卡(Martin Melka)

我想要一个基本的受身份验证保护的REST应用。我按照http://www.baeldung.com/spring-security-authentication-provider上的一般说明进行操作,以使安全性正常运行。

我最终创建了的实现AuthenticationProvider,但Spring从未调用过它。所有请求最终都会出现错误:

{"timestamp":1460199213227,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/test"}

无需AuthenticationProvider做任何事情。

该应用程序基于注释,以下是相关的位:

安全设定

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    @Autowired
    CustomAuthenticationProvider authenticationProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authenticationProvider(authenticationProvider)
                .authorizeRequests()
                .anyRequest().authenticated().and().httpBasic();
    }
}

身份验证提供者

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private Authenticator authenticator;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // This never gets called, I checked with debugger
        String username = authentication.getName();
        String password = authentication.getCredentials().toString();

        User user = userDAO.findByUsername(username);
        User authenticatedUser = authenticator.authenticate(user, password);
        if (authenticatedUser == null){
            throw new RESTAuthenticationException("Auth failed");
        }

        List<GrantedAuthority> authorityList = new ArrayList<>();
        return new UsernamePasswordAuthenticationToken(user, authorityList);
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.equals(UsernamePasswordAuthenticationToken.class);
    }
}

控制器

@RestController
public class UserController {
    @RequestMapping(value = "/test")
    public ResponseEntity test(@AuthenticationPrincipal User user) {
        return ResponseEntity.ok().body(user);
    }
}
本75

您收到状态码为401的响应。这是“未经授权”的http状态码这可能是由于您的请求中的Authorization标头缺失/格式不正确造成的。

您正在使用Http-Basic:它在请求中需要以下标头:

Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l

其中字符串QWxhZGRpbjpPcGVuU2VzYW11<user>:<password>编码base64的字符串

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义ArrayAdapter getFilter()不被调用

来自分类Dev

Spring Security自定义AuthenticationProvider被调用两次并失败

来自分类Dev

Spring Security - 自定义登录表单在提交时不调用 AuthenticationProvider

来自分类Dev

在scrollView的自定义视图中触摸进行缩放。touchesMoved不被调用

来自分类Dev

自定义pycurl调用

来自分类Dev

定义和调用自定义函数

来自分类Dev

Spring Security Java Config-自定义AuthenticationProvider和UserDetailsService

来自分类Dev

如何使用Spring Security OAuth2在Spring Boot中注册自定义BasicAuthenticationFilter AuthenticationProvider

来自分类Dev

春季安全-自定义AuthenticationProvider不起作用-Java配置

来自分类Dev

Symfony 4.通过LDAP授权。如何自定义AuthenticationProvider?

来自分类Dev

从自定义类调用presentViewController

来自分类Dev

从自定义JavaScript调用触发方法

来自分类Dev

在简码中调用自定义函数

来自分类Dev

从Java调用自定义R函数

来自分类Dev

WebApi自定义JsonConverter未调用

来自分类Dev

羽毛调用自定义API方法

来自分类Dev

NSTrackingArea调用自定义代码

来自分类Dev

自定义UserNamePasswordValidator未调用

来自分类Dev

未从自定义类调用cellForRowAtIndexPath

来自分类Dev

自定义注释未调用

来自分类Dev

自定义指令调用问题

来自分类Dev

粗俗的自定义方法未调用

来自分类Dev

自定义InfoWindowAdapter不从onMarkerClick()调用

来自分类Dev

WebGrid自定义异步调用

来自分类Dev

从Java调用自定义R函数

来自分类Dev

调用wordpress自定义页面元素

来自分类Dev

调用显示表单的自定义函数

来自分类Dev

角度自定义服务调用失败

来自分类Dev

在Woocommerce中调用自定义图标

Related 相关文章

热门标签

归档