foreverxunyc
forever_cherry
2017-03-24 03:18

Spring Security 无法进入UserServiceDetails方法

  • spring
  • userdetailsservice

项目启动不报错,直接在浏览器中输入/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条回答