登录弹簧安全和弹簧启动弹簧4后重定向到同一页面

时间:2017-09-11 09:21:47

标签: java jquery spring spring-boot spring-security

嗨,我是春天安全新手

我想在登录之前不要去主页我使用jquery来使用登录网络服务每一件事情都很顺利,直到我使用春季安全我不能用jquery来管理它我不知道该怎么办任何人都可以给出建议 如果我把" / home"在ant macher中,每件事都运行正常,但我需要先登录 我看到关于这个主题的所有stackoverflow问题,并且所有这些问题都是无用的

我的控制器

@ResponseBody
@RequestMapping(value="/index/login",method = RequestMethod.POST )
public Response login(@RequestBody LoginData loginData){
    System.out.println("Test Ahmed Test");
    Response response = new Response(loginRepo.validateLogin(loginData) , "home");
    System.out.println("Test Ahmed Test");
    return response;
}

我的安全配置

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
DataSource dataSource;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("select username,password, enabled from twitter.login where username=?")
            .authoritiesByUsernameQuery("select username, role from twitter.user_roles where username=?");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();

    http.formLogin()
            .loginPage("/index")
            .usernameParameter("username")
            .passwordParameter("password")
            .permitAll()
            .and()
            .authorizeRequests()
            .antMatchers(
            "/css/**",
            "/CSS/**",
            "/js/**",
            "/jquery/**",
            "/fonts/**",
            "/JS/**",
            "/img/**",
            "/webjars/**",
            "/**/favicon.ico",
            "/index/**").permitAll()
            .anyRequest().fullyAuthenticated()
            .antMatchers("/home").hasAuthority("ADMIN")
            .and().exceptionHandling().accessDeniedPage("/error/403");

}

}

我的jquery ajax功能

                $.ajax({

                contentType: 'application/json; charset=UTF-8',

                type : "POST",

                 cache : false,

                 crossDomain :false,

                url : url+'/login',

               data : JSON.stringify(formData),

              dataType : 'json',

             success : function(result) {

            console.log('hiiiiiiiiiii'+result.urlName)
            if(result.status == true){
                window.location.href= result.urlName;
            }else{
                alert("Error! Login Error Please Enter Vaild UserName and Password")
            }
            console.log(result);

        },

        error : function(e) {
            alert("Error!")
            console.log("ERROR: ", e);
        }
    });         

和我的回复模型

private boolean status;

private String urlName;

public Response(boolean status , String urlName){
    this.status = status;
    this.urlName=urlName;
}

public boolean isStatus() {
    return status;
}

public void setStatus(boolean status) {
    this.status = status;
}

public String getUrlName() {
    return urlName;
}

public void setUrlName(String urlName) {
    this.urlName = urlName;
}
}

0 个答案:

没有答案