登录不适用于UserDetailsS ervice和自定义登录

问题描述 投票:0回答:1

我正在尝试使用带有自定义登录表单的UserUsersService类进行身份验证。但是似乎正在执行UserDetailsS​​ervice的UserDetailsS​​erviceImp类没有调用。

第二我在调用表单Login.jsp的路径的控制器处理中应该返回什么?

这是我的UserDetailsS​​erviceImp代码。

@Component
public class UserDetailsServiceImp implements UserDetailsService {
    private static Logger logger = LogManager.getLogger(AuthService.class);
    @Autowired
    EntityManager em;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = findUserbyUername(username);
        System.out.println("Called service");`enter code here`
        UserBuilder builder = null;
        if (user != null) {
            builder = org.springframework.security.core.userdetails.User.withUsername(username);
            builder.password(new BCryptPasswordEncoder().encode(user.getPassword()));
            builder.roles(user.getRoles());
        } else {
            throw new UsernameNotFoundException("User not found.");
        }

        return builder.build();
    }

    private User findUserbyUername(String username) {
        if (username.equalsIgnoreCase("admin")) {
            return new User(username, "admin123", "ADMIN");
        }
        return null;
    }
}

下面是我的自定义JSp页面。

    <div id="container">
        <form class="form-horizontal" action="validate" method="POST">
            <div class="row">
                <div class="Absolute-Center is-Responsive">
                    <div id="logo-container"></div>
                    <div class="col-sm-2 col-md-offset-5">
                        <p>
                        <div class="form-group text-center">
                            <c:if test="${fn:length(output)>0}">
                                <c:if test="${fn:contains(output, 'successfully')}">
                                    <div class="alert alert-success" id="message">
                                        <strong>Success!</strong> ${output}
                                    </div>
                                </c:if>
                                <c:if test="${!fn:contains(output, 'successfully')}">
                                    <div class="alert alert-danger" id="message">
                                        <strong>Alert!</strong> ${output}
                                    </div>
                                </c:if>
                            </c:if>
                        </div>
                        <div class="form-group input-group">
                            <span class="input-group-addon"><i
                                class="glyphicon glyphicon-user"></i></span> <input
                                class="form-control" type="text" name='username' required
                                placeholder="Enter User ID" />
                        </div>
                        <div class="form-group input-group">
                            <span class="input-group-addon"><i
                                class="glyphicon glyphicon-lock"></i></span> <input
                                class="form-control" type="password" name='password' required
                                placeholder="Enter password" />
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-def btn-block btn-primary">Login</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>

I am calling "validate" path in this jsp with POST Request.Below is my controller class to handle this request.

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

[如果我在此方面有任何错误,请告诉我。

谢谢阿弥陀佛

请查找我的配置类别。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        System.out.println("Inside 2");
        web.ignoring().antMatchers("/resources/**");
    }

    @Autowired
    UserDetailsServiceImp userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        System.out.println("Inside 3");
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/", "/css/**", "/js/**", "/images/**", "/fonts/**", "/j_spring_security_check").permitAll()
                .anyRequest().authenticated()
                .and().formLogin().loginPage("/")
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/welcome").permitAll()
                .and()
                .logout().logoutUrl("/logOut").permitAll();

        System.out.println("Inside 1");
    }
}`enter code here`
spring security
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.