当我尝试运行我的 WEB 应用程序时,我收到此错误 [重复]

Posted

技术标签:

【中文标题】当我尝试运行我的 WEB 应用程序时,我收到此错误 [重复]【英文标题】:When I try to run my WEB Application I get this error [duplicate] 【发布时间】:2016-04-09 22:54:48 【问题描述】:

2016 年 1 月 5 日 23:44:35.610 严重 [http-apr-8080-exec-2] org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.doFilter 尝试对用户进行身份验证时发生内部错误。

org.springframework.security.authentication.InternalAuthenticationServiceException 引起:java.lang.NullPointerException 在 dao.UserDAOImpl.getUser(UserDAOImpl.java:36) 在 service.UserDetailsS​​erviceImpl.loadUserByUsername(UserDetailsS​​erviceImpl.java:38) 在 org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:102)

我的 web.xml

<display-name>Spring MVC Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/mvc-dispatcher-security.xml
        /WEB-INF/mvc-dispatcher-servlet.xml

    </param-value>

</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

安全性.xml

   <security:http auto-config="true"  >


          <security:intercept-url pattern="/admin-panel" access="ROLE_ADMIN"/>
          <security:intercept-url pattern="/user_panel" access= "ROLE_USER"/>


           <security:form-login login-page="/welcome"

                                default-target-url="/default"
                                username-parameter="username"
                                password-parameter="password"/>


   </security:http>



          <bean  class="service.UserDetailsServiceImpl" id="userDetailsService" autowire="byType">


   </bean>
   <security:authentication-manager  >
          <security:authentication-provider user-service-ref="userDetailsService">


          </security:authentication-provider>
   </security:authentication-manager>

servlet.xml

<context:component-scan base-package="springapp.mvc,service,dao,domain"/>

<context:annotation-config/>


    <mvc:annotation-driven/>

    <mvc:default-servlet-handler />

    <mvc:resources mapping="/resources/**" location="resources/">
    </mvc:resources>



<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>



    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource">

        </property>
    </bean>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/votingsystem"/>
        <property name="username" value="root"/>
        <property name="password" value="anl14anl14"/>
    </bean>

用户.java

package domain;

public class User



    private String username;
    private String password;
    private String role;




    public User(String username, String password,String role ) 
    

        this.username = username;
        this.password = password;
        this.role=role;

    

    public User() 

    

    public boolean enter(String username, String password) 
        this.username = username;
        this.password = password;
        return true;
    

    public String getUsername() 
        return username;
    

    public void setUsername(String username) 
        this.username = username;
    

    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    

    public String getRole() 
        return role;
    

    public void setRole(String role) 
        this.role = role;
    

UserDAO.java

package dao;

import domain.User;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;


public interface UserDAO


    public User getUser(String username);



UserDAOImpl.java

package dao;

import domain.User;
import domain.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
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.Repository;





@Repository
public class UserDAOImpl implements UserDAO




    private JdbcTemplate template;

    private static final String find = "SELECT role FROM users WHERE username = ? ";

    public UserDAOImpl() 

    

    @Override
    public User getUser(String username) 


        return template.queryForObject(find, new Object[]username, User.class);
    



UserDetailsS​​erviceImpl.java

package service;

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;


@Service
public class UserDetailsServiceImpl implements UserDetailsService



@Autowired
UserDAO userDAO;



    @Override
    public UserDetails loadUserByUsername(String username)

            throws UsernameNotFoundException


    
        User user =  userDAO.getUser(username);

        Set<GrantedAuthority> roles = new HashSet();
        if (user.getRole().equals("admin"))
            roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        else if (user.getRole().equals("user"))
            roles.add(new SimpleGrantedAuthority("ROLE_USER"));

      UserDetails userDetails =new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),roles);
        return userDetails;


         

    

Controller.java

package springapp.mvc;

import dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller

public class HelloController




    @RequestMapping (value = "/welcome")


    public String get ( Model model )
    

        return "welcome";
    


        @RequestMapping (value = "/default")

        public String adminPage ( HttpServletRequest request) 

     if (request.isUserInRole("ROLE_ADMIN")) 

         return "redirect:/admin-panel";

     

     else if (request.isUserInRole("ROLE_USER"))
     
         return "redirect:/user_panel";
     


    return "redirect:/welcome";
 

    @RequestMapping (value = "/user_panel")

    public ModelAndView regPage ()
    
        ModelAndView modelAndView  = new ModelAndView();
        modelAndView.setViewName("user_panel");
        return modelAndView;


    

    @RequestMapping (value = "/admin-panel")

    public ModelAndView adminPage ()
    
        ModelAndView modelAndView  = new ModelAndView();
        modelAndView.setViewName("admin-panel");
        return modelAndView;


    
    

welcome.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>

<head>
<title> Hello </title>
<meta charset="utf-8">

<c:url value="/resources/theme/css/main1.css" var="main"/>

    <link href="$main" rel="stylesheet"/>



</head>

<body>



<div id="wrapper" >

<div id="header">

 <h1> ACCESS TO ELECTIONS  </h1>

</div>

    <div id="form_user">

    <div id="registration "> </div>



        <div class="form">
            <form method="post">
                <div class="youInfo">
                    <div class="info youName">
                        <label for="name">Name<input type="text" id="name" required /><span></span></label>



                        <label for="login">Login<input type="email" id="login" required /><span></span></label>
                        <label for="password1">Password<input type="password" id="password1" required /><span></span></label>

                        <button type="submit">Register</button>
                    </div>


             </div>
                </form>
        </div>

        <p id="part">  </p>

        <div id="enter"> </div>

        <div class="form">
            <form action="/j_spring_security_check" method="post"  >
                <div class="enter">
                    <div class="enterin">
                        <label for="username">Login<input type="text" name="username" id="username"/><span></span></label>
                        <label for="password">Password <input type="password" name="password" id="password"  /><span></span></label>

                        <button type="submit">Sign in</button>
                    </div>


                </div>
            </form>
        </div>

    </div>


    <div id ="footer"> </div>


    </div>




</body>
</html>

【问题讨论】:

UserDAOImpl.java:36 是哪一行? @Abdelhak:很可能是return 行。 因为UserDAOImpl 中的字段template 为空(从未分配)。 朋友们,我已经决定了这些问题。有必要更改SQL脚本“SELECT role FROM users WHERE username = ?”;到“SELECT * FROM users WHERE username = ?” 因为 wnen 我们使用 UserDetails userDetails =new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),roles);返回用户详细信息;在这个例子中它还需要“密码”和“角色” 【参考方案1】:

您可能在 UserDAOImpl.java 中缺少 private JdbcTemplate template; 上的 @Autowired

@Repository
public class UserDAOImpl implements UserDAO 
    @Autowired
    private JdbcTemplate template;

    ...

【讨论】:

朋友们,我已经决定了这些问题。有必要更改SQL脚本“SELECT role FROM users WHERE username = ?”;到 "SELECT * FROM users WHERE username = ?" 因为 wnen 我们使用 UserDetails userDetails =new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPa‌​ssword(),roles);返回用户详细信息;在这个例子中它还需要“密码”和“角色”

以上是关于当我尝试运行我的 WEB 应用程序时,我收到此错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

我收到此错误消息:用户访问被拒绝(使用密码是)

为啥我在尝试运行我的 Twitter 天气机器人时会收到此错误

我在运行我的应用程序(Flutter、FireBase)时收到此 E/flutter (25055) 错误

每次我尝试在我的 android 设备中运行代码时都会收到此错误。我是应用程序开发的初学者。我正在使用 VScode

当我尝试在我的终端上运行代码时,我不断收到“ModuleNotFound”错误,即使我安装了它

为啥我在运行动态 Web 项目时收到 HTTP 404 错误? [复制]