使用Basic Auth编写每个请求Spring Boot



我正在构建一个具有不同路径的RESTapi,用于控制从移动应用程序输入的数据(你猜到了,它扮演着前端的角色(。我仍处于应用程序开发的第一阶段,现在我正在测试我的授权会话。我选择了基本身份验证(httpBasic((-作为方法的名称(,我希望移动应用程序对服务器的每一个请求都经过身份验证。因为,目前,如果我进行一次身份验证,那么下次就不需要发送身份验证数据。这可能吗?这是授权的功能:

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/**").hasAuthority("ROLE_USER")
.anyRequest().authenticated()
.and()
.httpBasic()
.and().logout()
.clearAuthentication(true)
.invalidateHttpSession(true)
.logoutSuccessUrl("/")
.permitAll();
}

您可以编写自定义Success Handler来处理它。

类似:

.logout()
.logoutSuccessHandler(new LogoutSuccessHandler() {
@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
CustomerUserDetails userDetails = (CustomerUserDetails) authentication.getPrincipal();
String username = userDetails.getUsername();
System.out.println("The user " + username + " has logged out.");
response.sendRedirect(request.getContextPath());
}
})
.permitAll();

检查一下-这里是

最新更新