forever_cherry 2017-03-24 03:18 采纳率: 33.3%
浏览 8052

Spring Security 无法进入UserServiceDetails方法

项目启动不报错,直接在浏览器中输入/spitter/home也会直接跳转到spitter/login页面,但是一但提交表单,会直接进入到后台login方法,而不会进入到UserDetailsService方法中,而且也不会按照方法正常执行的那样跳转到home页面,而是返回到了login页面,求解。。。。。

 package cherry.config;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

// 配置DelegatingFilterProxy
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer{

}

 package cherry.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private UserDetailsService spitterUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.formLogin().loginPage("/spitter/login").permitAll()
            .and().rememberMe()
            .and().authorizeRequests().antMatchers("/spitter/home").hasRole("SPITTER")
            .anyRequest().permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(spitterUserDetailsService);
    }

}

 package cherry.config.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import cherry.model.Spitter;
import cherry.repository.SpitterRepository;

@Service
public class SpitterUserDetailsService implements UserDetailsService {

    @Autowired
    private SpitterRepository spitterRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.err.println("---------- User Details Service start -----------");
        Spitter spitter = spitterRepository.findByUsername(username);
        if(spitter == null){
            throw new UsernameNotFoundException(username + " not found ");
        }
        List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority("ROLE_SPITTER"));
        System.out.println(spitter.getUsername() + " : ROLE_SPITTER");
        return new User(spitter.getUsername(), spitter.getPassword(), authorities);
    }

}

 package cherry.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cherry.model.Spitter;
import cherry.repository.SpitterRepository;

@Controller
@RequestMapping("/spitter")
public class SpitterController {

    @Autowired
    private SpitterRepository spitterRepository;

    @RequestMapping(value= "/login", method = RequestMethod.GET)
    public String login(Model model){
        model.addAttribute("spitter", new Spitter());
        return "spitter/login";
    }

    @RequestMapping(value= "/loginForm", method = RequestMethod.POST)
    public String login(@ModelAttribute("spitter") Spitter spitter){
        System.out.println(spitter.getUsername() + " logined..");
        return "redirect:home";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String showRegistrationForm(Model model){
        model.addAttribute("spitter", new Spitter());
        return "spitter/registerForm";
    }

    @RequestMapping(value="/register", method = RequestMethod.POST)
    public String register(@ModelAttribute("spitter") Spitter spitter){
        spitterRepository.addSpitter(spitter);
        return "spitter/registerSuccessfully";
    }

    @RequestMapping("/home")
    public String hello(){
        return "spitter/helloWorld";
    }
}

附:https://github.com/CherryYu/SpringHibernate.git

  • 写回答

2条回答

  • HeiBoyYang 2017-03-24 03:27
    关注

    断点检查:spitterUserDetailsService是否为null,如果是null,需要检查配置文件包扫描scan

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记