ztl000
ztl000
采纳率100%
2018-03-03 14:25 阅读 8.1k
已采纳

spring boot login返回302

80

spring boot + spring security

之前没有遇到过这个问题,今天晚上登录的时候突发,本人刚入门spring boot 求大佬赐教
图片说明

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

@Bean
UserDetailsService customUserService(){ //CustomUserService注册bean
    return new CustomUserService(); 
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(customUserService()); //添加自定义认证

}

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

    http.authorizeRequests()
                    .antMatchers("/js/**").permitAll()
                    .antMatchers("/css/**").permitAll()
                    .antMatchers("/bootstrap/**").permitAll()
                    .antMatchers("/fonts/**").permitAll()
                    .antMatchers("/favicon.ico").permitAll()
                    .anyRequest().authenticated() //4
                    .and()
                    .formLogin()
                        .loginPage("/login")
                        .defaultSuccessUrl("/word")
                        .failureUrl("/login?error")
                        .permitAll() //登陆页面可任意访问
                    .and()
                    .logout().permitAll(); //注销页面可任意访问



}

}

public class WebMvcConfig extends WebMvcConfigurerAdapter{

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");//注册访问/login转向login.html页面
}

}

控制层:

@Controller
public class HomeController {

@RequestMapping("/word")
public String index(Model model){
    return "home";
}

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

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2018-03-03 15:47

    跨域问题,设置请求头 withCredentials = ture

    点赞 评论 复制链接分享
  • ztl000 ztl000 2018-03-03 15:31

    忘了给前端:

    <!DOCTYPE html>



    登录页面
    body { padding-top: 50px; } .starter-template { padding: 40px 15px; text-align: center; }


     <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">留言簿</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
           <li><a th:href="@{/}"> 登陆 </a></li>
    
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
     <div class="container">
    
      <div class="starter-template">
       <p th:if="${param.logout}" class="bg-warning">已成功注销</p><!-- 1 -->
            <p th:if="${param.error}" class="bg-danger">有错误,请重试</p> <!-- 2 -->
            <h2>使用账号密码登录</h2>
            <form name="form" th:action="@{/login}" action="/login" method="POST"> <!-- 3 -->
                <div class="form-group">
                    <label for="username">账号</label>
                    <input type="text"  class="form-control" name="username" value="" placeholder="账号" />
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" class="form-control" name="password" placeholder="密码" />
                </div>
                <input type="submit" id="login" value="Login" class="btn btn-primary" />
            </form>
      </div>
    
    </div>
    


    点赞 评论 复制链接分享
  • dom_ee dom_ee 2018-03-05 02:04

    忘了给前端
    <!DOCTYPE html>
    楼主很细心啊

    点赞 评论 复制链接分享

相关推荐