Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java REST服务是否有Spring安全扩展?_Java_Spring_Rest_Spring Boot_Spring Security - Fatal编程技术网

Java REST服务是否有Spring安全扩展?

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构建RESTAPI,并使用SpringSecurity。我开始的时候发现了一些关于这个问题的其他教程和博客文章,并在实现了自定义内容后成功地使它工作起来。帖子回答了我的一些问题,但我还有一个问题:

是否有任何扩展实现了一些功能,比如REST AuthenticationEntryPoint返回401而不是重定向,或者JWT生成和验证,或者我应该为每个REST服务实现相同的功能


感谢您的回答。

我也使用Springboot,但出于安全考虑,我基本上依赖于存储用户帐户的方式(我的帐户在MongoDb实例中)

  • 负责login-currentUser.login(令牌)
  • 如果失败,则抛出异常,以便处理响应
  • 如果成功,则在响应中插入身份验证cookie
  • 对于任何其他请求,解码cookie并向用户注入适当的授权
简单地说,Shiro不会重定向HTTPRequest,因为它只关心安全性,而将进一步的决定重定向到控制器逻辑。 您可以通过简单的Maven依赖将其添加到项目中。

@brownies。。。。。 试试这个

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()......