小编kak*_*ali的帖子

在 Spring 5.0 中设置 Spring 的 RestTemplate 的基本 URL/URI

RestTemplate在 Spring Boot 2 应用程序中使用 Spring 5,并尝试在其上设置基本 URL/URI,这样我就不必在每个请求前面添加它。这也允许我根据属性设置该值。

以前的版本ReleaseTemplate允许您通过构造函数设置基本 url(例如new ReleaseTemplate(baseUrl))。但现在情况已不再如此。

我看到该类DefaultUriTemplateHandler有一个setBaseUrl()AbstractUriTemplateHandler接口继承的类,但该类已被弃用。建议的替换类DefaultUriBuilderFactory没有任何此类方法。

任何建议,将不胜感激。谢谢。

java spring spring-boot

2
推荐指数
1
解决办法
4988
查看次数

如何将 JAVA_OPTS 传递给 docker 容器中的 tomcat?

我想使用特定的配置文件运行我的 spring-boot 应用程序。因此,我需要将-Dspring.profiles.active=dev参数传递给 JVM。

我的 dockerfile 如下所示:

FROM tomcat:9.0.10-jre8

EXPOSE 8080
ENV JAVA_OPTS="-Dspring.profiles.active=dev"
COPY myapp.war /usr/local/tomcat/webapps/myapp.war
Run Code Online (Sandbox Code Playgroud)

应用程序启动,但带有配置文件名称的 JVM 选项似乎未设置。我缺少什么?

java tomcat docker spring-boot dockerfile

2
推荐指数
1
解决办法
9209
查看次数

Spring Boot 2 安全基础认证

为什么以下基本安全配置不适用于 inMemoryAuthentication() 子句?

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("username").password("password");
        super.configure(auth);
    }

}
Run Code Online (Sandbox Code Playgroud)

应用初始化之后,仍然只有userSpring自己生成的default ,没有像username.

java spring spring-security spring-boot

1
推荐指数
2
解决办法
1万
查看次数

Spring MVC - 重定向后获取模型数据

我想在重定向后将数据传递到视图。例如,我按下一个按钮,它会将我重定向到包含来自控制器的数据的页面。我正在尝试使用每个人都建议的 RedirectAttribute,但我无法让它工作。任何帮助表示赞赏。

索引.jsp:

<a href="user.htm">Display All Users</a>
Run Code Online (Sandbox Code Playgroud)

控制器:

@RequestMapping(value = "/user.htm")
public ModelAndView addUser(RedirectAttributes redirAtt) {
    ModelAndView mv = new ModelAndView("redirect:user");
    String out = "All User Details: ";
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        List result = session.createQuery("from Users").list();
        mv.addObject("users", result);
        session.getTransaction().commit();

    } catch (Exception e) {
        e.printStackTrace();
    }

    redirAtt.addFlashAttribute("message", out);
    return mv;
}
Run Code Online (Sandbox Code Playgroud)

我想要在上面显示数据的jsp:user.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${message}</title>
    </head>
    <body>
        <h1>${message}</h1><br>
        <table>
            <tr>
                <th>Username</th>
                <th>Nickname</th>
                <th>Email</th> …
Run Code Online (Sandbox Code Playgroud)

java spring jsp controller spring-mvc

1
推荐指数
1
解决办法
1万
查看次数

如何将 Spring MVC 应用程序与具有 OAuth2.0 安全性的 Spring Cloud Gateway 集成?

我有一个 Spring Boot MVC + Thymeleaf 应用程序,它通过 Spring Cloud Gateway 与一堆微服务进行通信。

mvc_app -> gateway -> (service_a, service_b,...)

现在,我想实现基于 OAuth 2 的安全性,并且希望将身份验证过程集中在网关级别。

这意味着:

  1. 如果用户尝试通过网关访问任何资源服务器(service_a、service_b 等)上的安全资源,网关应将用户重定向到 IdP 登录页面。
  2. 身份验证成功后,网关应使用 accessToken 来访问受保护的资源。

这可以通过以下方式实现:

  1. spring-boot-starter-oauth2-client使用适当的提供者/客户端配置将网关设置为 OAuth 2 客户端
  2. 使用TokenRelay过滤器将AccessToken转发到下游resource_servers。

问题:

当您通过网关进行身份验证时,身份验证状态将在网关应用程序级别维护。网关成功验证后,如何发回 AccessToken、IdToken 以便mvc_app进一步请求时使用相同的 AccessToken?

spring-security oauth-2.0 spring-boot spring-cloud-gateway

1
推荐指数
1
解决办法
421
查看次数