简体   繁体   English

使用带有角度的 spring boot 登录页面使用 spring 安全

[英]Using spring boot with angular for login page using spring security

I am trying to implement a login and welcome page using Angular as the front end and spring boot as the controller .我正在尝试使用 Angular 作为前端和 spring boot 作为控制器来实现登录和欢迎页面。

In my WebSecuirtyConfigureAdapter在我的 WebSecuirtyConfigureAdapter 中

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.httpBasic().and()
        .authorizeRequests()
            .antMatchers("/index.html").permitAll()
            .antMatchers("/src/assets/logo.png").permitAll()
            .antMatchers("/main.js").permitAll()
            .antMatchers("/main.js.map").permitAll()
            .antMatchers("/polyfills.js").permitAll()
            .antMatchers("/polyfills.js.map").permitAll()
            .antMatchers("/styles.js").permitAll()
            .antMatchers("/styles.js.map").permitAll()
            .antMatchers("/vendor.js").permitAll()
            .antMatchers("/vendor.js.map").permitAll()
            .antMatchers("/runtime.js.map").permitAll()
            .antMatchers("/runtime.js").permitAll()
            .antMatchers("/saml/**").permitAll()
            .antMatchers("/favicon.ico").permitAll()
            .antMatchers("/assets/logo.png").permitAll()
            .antMatchers("/oauth/**").permitAll()
            .antMatchers("/userinfo").permitAll()
            .antMatchers("/").permitAll()
        .anyRequest()
            .authenticated()
        .and()
            .formLogin().loginPage("/#/login").permitAll()
            .successHandler(this.successHandler)
            .failureHandler(this.failureHandler)
        .and()
            .exceptionHandling()
            .authenticationEntryPoint(this.entryPoint)
        .and()
            .csrf().csrfTokenRepository(csrfTokenRepository()).ignoringAntMatchers("/url5/**")
        .and()
            .addFilterAfter(filter, BasicAuthenticationFilter.class)
            .addFilterBefore(filter, CsrfFilter.class)
            .addFilterAfter(this.csrfHeaderFilter(), CsrfFilter.class);
}

I need to re-direct any unauthenticated requests to authenticationEntryPoint but since I had to add "/" to allow Angular to work it is not working as expected .我需要将任何未经身份验证的请求重定向到 authenticationEntryPoint 但由于我必须添加“/”以允许 Angular 工作,因此无法按预期工作。 As "/" is permitted every request is permitted and I do not hit the entryPoint which directs me to the login page at /#/login.由于允许“/”,因此每个请求都被允许,并且我没有点击将我定向到 /#/login 处的登录页面的入口点。 If I remove the "/" I am able to hit the entryPoint but Angular wouldn't render as spring security would not allow that to load .如果我删除“/”,我可以点击 entryPoint 但 Angular 不会呈现,因为 Spring Security 不允许加载。 I need my unauthorized request to go via the entry point as this is a legacy code and it works the parameters which might be sent along with the request.我需要我未经授权的请求通过入口点,因为这是一个遗留代码,它可以处理可能与请求一起发送的参数。 Can you please help me how to achieve the same ?你能帮我如何实现同样的目标吗?

Try adding a Controller which will forward to index.html .尝试添加一个Controller ,它将转发到index.html

@Controller
public class UiController {

    @RequestMapping({
            "/",
            "/login", //my login page is on /login
    })
    public String handler() {
        return "forward:index.html";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM