Java REST服务是否有Spring安全扩展?
我正在用SpringBoot构建RESTAPI,并使用SpringSecurity。我开始的时候发现了一些关于这个问题的其他教程和博客文章,并在实现了自定义内容后成功地使它工作起来。帖子回答了我的一些问题,但我还有一个问题: 是否有任何扩展实现了一些功能,比如REST AuthenticationEntryPoint返回401而不是重定向,或者JWT生成和验证,或者我应该为每个REST服务实现相同的功能Java REST服务是否有Spring安全扩展?,java,spring,rest,spring-boot,spring-security,Java,Spring,Rest,Spring Boot,Spring Security,我正在用SpringBoot构建RESTAPI,并使用SpringSecurity。我开始的时候发现了一些关于这个问题的其他教程和博客文章,并在实现了自定义内容后成功地使它工作起来。帖子回答了我的一些问题,但我还有一个问题: 是否有任何扩展实现了一些功能,比如REST AuthenticationEntryPoint返回401而不是重定向,或者JWT生成和验证,或者我应该为每个REST服务实现相同的功能 感谢您的回答。我也使用Springboot,但出于安全考虑,我基本上依赖于存储用户帐户的方式
感谢您的回答。我也使用Springboot,但出于安全考虑,我基本上依赖于存储用户帐户的方式(我的帐户在MongoDb实例中)
- 负责login-currentUser.login(令牌)李>
- 如果失败,则抛出异常,以便处理响应
- 如果成功,则在响应中插入身份验证cookie
- 对于任何其他请求,解码cookie并向用户注入适当的授权
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@Component
public class RESTAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
}
}
在安全配置类中添加上述重新身份验证入口点和配置,如果身份验证失败,它将返回401。
@Autowired
private RESTAuthenticationEntryPoint restAuthenticationEntryPoint;
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.cors().and().exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint).and().authorizeRequests()......