Spring Boot Security自定义身份验证提供程序不适用于自定义登录页面

时间:2018-11-25 18:16:24

标签: spring-boot spring-security

我已经实现了一个Spring Boot应用程序来使用自定义身份验证提供程序。当我使用httpbasic身份验证模式时,该功能可以正常工作。但是,当我使用自定义登录页面时,在访问任何页面之前都会显示登录页面。但是登录后,它将停留在同一页面上。  使用httpbasic身份验证模式时会调用在我的自定义身份验证提供程序中放入的打印语句,但在使用自定义登录页面时不会调用。下面是代码

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private CustomAuthenticationProvider aCustomAuthenticationProvider;

    @Autowired
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(aCustomAuthenticationProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
        .antMatchers("/mybank/**").permitAll()
        .anyRequest().authenticated()
        .and()
        .httpBasic()
        //.formLogin()
        //.loginPage("/login")
        //.defaultSuccessUrl("/loginUser")
        .and()
        .logout()
        .permitAll();
    }

}

    @Component
public class CustomAuthenticationProvider implements AuthenticationProvider{

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String uname = authentication.getName();
        String password = authentication.getCredentials().toString();
        System.out.println("User Name: "+uname);
        System.out.println("Password: "+password);
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths);
    }


    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}


    @RequestMapping("/login")
    public ModelAndView loginPage()
    {
        Customer cust = new Customer();
        ModelAndView mv = new ModelAndView();
        mv.addObject("customer", cust);
        mv.setViewName("login");
        return mv;
    }

    @RequestMapping("/loginUser")
    public String login(@ModelAttribute("customer")Customer cust){
        return "homepage";
    }

My login.jsp is as below

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>
<head></head>
<body>
    <h3>Welcome, Please Login</h3>
    <form:form method="POST" action="/mybank/loginUser"
        modelAttribute="customer">
        <table>
            <tr>
                <td><form:label path="custId">Customer ID</form:label></td>
                <td><form:input path="custId" /></td>
            </tr>

            <tr>
                <td><form:label path="password">Password</form:label></td>
                <td><form:password path="password" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

0 个答案:

没有答案
相关问题