反向代理背后的Spring安全登录

时间:2016-03-22 16:04:35

标签: apache spring-mvc tomcat spring-security proxy

我使用Spring MVC 4和ThymeLeaf开发了我的Spring应用程序。 我已经使用Spring Security 3获得了它。 如果我有直接访问权限,一切都很好,但如果我想创建一个反向代理来管理连接,那么Spring Security无法正常工作。

我的Spring Security配置是:

http
    .csrf().disable()
.formLogin().loginPage("/html/login").defaultSuccessUrl("/html/index", true).permitAll()
            .and()
            .logout().logoutUrl("/html/logout").logoutSuccessUrl("/html/login?logout").permitAll()
            .and()
            .authorizeRequests()
            .antMatchers("/html/res/**").permitAll()
            .antMatchers("/html").authenticated()
            .antMatchers("/html/**").authenticated();

csrf已禁用,因为它不能与反向代理一起使用。 我在Apache 2.4上使用mod_proxy_http的反向代理代码是

ServerName example.com
ProxyPass / http://localhost:8080/myapp/html/
ProxyPassReverse / http://localhost:8080/myapp/html/

我可以看到登录页面,但是当我点击"登录"而不是http://example.com/index时,我有http://example.com/myapp/html/。 我认为这是一个与Spring Security需要的绝对路径相关的问题,但我不知道如何解决这个问题。

更新 我使用了解决方法......我将其添加到我的apache站点conf文件中:

RewriteEngine on
RewriteRule ^/myapp/html/(.*)$ /$1 [PT]

因此它删除了地址的重复部分。但我不喜欢这样。 我的登录页面是:

<div class="container">

    <div class="modal-header" align="center">
        <img class="img-circle" id="img_logo" th:src="@{res/img/logo200x200.png}">
    </div>
    <form class="form-signin" th:action="@{login}" method="post">

        <div th:if="${param.error}">
            Failed to login.
            <div th:if="${SPRING_SECURITY_LAST_EXCEPTION != null}">
              Reason: <span th:text="${SPRING_SECURITY_LAST_EXCEPTION.message}"></span>
            </div>
        </div>

        <div th:if="${param.logout}">
            You have been logged out.
        </div>

        <label for="username" class="sr-only">Email address</label>
        <input type="email" id="username" name="username" class="form-control" placeholder="Email address" required autofocus>

        <label for="password" class="sr-only">Password</label>
        <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>

        <!--
        <div class="checkbox">
            <label>
                <input type="checkbox" value="remember-me"> Remember me
            </label>
        </div>
        -->
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    </form>

</div>

0 个答案:

没有答案
相关问题