在Spring安全扩展多租户应用程序

时间:2017-07-26 13:49:18

标签: spring spring-security multi-tenant

首先,如果构建的问题似乎不适合我要解释的问题,我表示道歉。

我正在开发基于弹簧的多租户应用程序,可以扩展弹簧安全性。 该应用程序是通过URL路径识别租户。 例如,3个不同的租户产品网址将如下所示

http://localhost:8084/webapp/companyA/products
http://localhost:8084/webapp/companyB/products
http://localhost:8084/webapp/companyB/products

我正在尝试将尝试访问任何公司的受限页面的未经身份验证的用户重定向到登录页面。 例如,未经身份验证的用户尝试访问

http://localhost:8084/webapp/companyA/admin

用户应该被重定向到

http://localhost:8084/webapp/companyA/login

我面临的问题是,即使用户被重定向到登录页面,我也无法将正确的URL返回给用户。 例如,如果使用companyA进行测试,则返回的浏览器地址栏上显示的URL为

http://localhost:8084/webapp/{tenant}/login

而不是

http://localhost:8084/webapp/companyA/login

我的代码

控制器类

@Controller
@RequestMapping(value = "/{tenant}")
public class DemoController {

    @RequestMapping(value="/login", method = RequestMethod.GET)  
    public String getLoginPage() {  

        return "login";  

    }

    @RequestMapping(value="/admin", method = RequestMethod.GET)  
    public String getAdminPage() {  

        return "admin";  

    }

}

Spring安全类

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/{tenant}/admin").access("hasRole('ROLE_ADMIN')")
        .and()
                .formLogin()
                .loginPage("/{tenant}/login")
                .loginProcessingUrl("/{tenant}/secured-login")
                .usernameParameter("username")
                .passwordParameter("password");

    // rest of the code

}

如果这种做法不正确,那么更好的方法将受到如此多的欢迎和赞赏。感谢

0 个答案:

没有答案