在Spring + hibernate中对DB进行输入验证

时间:2016-11-05 23:46:00

标签: spring hibernate validation spring-mvc passwords

它的春季MVC应用程序与Hibernate。

cross join

用户登录到填写此表单的login.jsp页面:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean save(User user) {

        return userDao.save(user);
    }

    @Override
    public void update(User user) {

        userDao.update(user);

        // return this.userDao.update(user);
    }

    @Override
    @Transactional
    public User findById(int id) {

        return this.userDao.findById(id);
    }

    @Override
    @Transactional
    public List<User> listPersons() {

        return this.userDao.listPersons();
    }

    @Override
    @Transactional
    public User deleteUser(int id) {

        return userDao.deleteUser(id);
    }

    public boolean validateUser(int id) {

        List<User> list= (List<User>) findById(id); 


        return false;   


    }

    public User validateUser(User user) {   

        Session session = this.sessionFactory.getCurrentSession();

        String query = "select u.name, u.password from User as u where u.name='"+ user.getName() + "' and u.password='"
                + user.getPassword() + "'";

        session.createQuery(query);       

        ResultSet rs = (ResultSet) session.createQuery (query);

            try {
                if (rs.next()){

                    return user;
                } else
                    return user;
            } catch (SQLException e) {

                e.printStackTrace();
            }
            return user;

        }
}
My UserServiceImpl class has a method to validate the user input[ password and userID];

表单转到了contrroller:

<body>

    Welcome back!

    <br> Only Registered user can log in...
    <br>
    <br>
    <form:form action="admin" modelAttribute="user" method="POST">
        <table border="1">

            <tr>
                <td><form:label path="userId">Your Id:- </form:label></td>
                <td><form:input path="userId" /></td>
            </tr>
            <tr>
                <td><form:label path="password">Password:- </form:label></td>
                <td><form:input path="password" /></td>
            </tr>

            <tr>
                <td><form:label path="role">Select Log in role as a:- </form:label></td>
                <td><form:select path="role">
                        <form:option value="NONE" lable="---SELECT---">Please Select</form:option>
                        <form:options items="${roles}" />
                    </form:select></td>
                <td><input type="submit" value="Login" /></td>
            </tr>
        </table>
    </form:form>
</body>

我的用户对象是

@RequestMapping(value = "/admin", method = RequestMethod.POST)
    public String LoggedUser(@ModelAttribute("user") User user, BindingResult result, Model model) {

        // get the role, id and pw value from jsp
        String role = user.getRole();
        String loadedPW = user.getPassword();
        String loadedUId = user.getUserId();
        // want to check password and userId here again Db
        //loadedPW.

      //directing to admin page and gen page
    if (role.equalsIgnoreCase("Admin") || role.equalsIgnoreCase("Principal")) {
            return "adminPage";         
        } else
            return "genPage";
    }

如何针对我的User表验证login.jsp上的密码和userID输入? 我是否必须使用JDBC Resultset或者还有其他更好的方法来验证用户输入?我在Spring MVC 4.x中使用Hibernate 4.3x。

1 个答案:

答案 0 :(得分:1)

  

如何验证login.jsp上的密码和userID输入   我的用户表?

您可以使用spring-security模块,该模块非常强大,可以进行身份​​验证。授权用户请求(例如在您的Web应用程序中),您可以找到示例here

spring-security模块提供了各种方法来配置内存,数据库,LDAP等用户详细信息。但是对于您的情况,您需要使用(AuthenticationManagerBuilder.jdbcAuthentication())进行JDBC身份验证。

方法是您需要通过覆盖configAuthentication

的方法configure()和WebSecurityConfigurerAdapter()方法来提供配置类
  

我是否必须使用JDBC Resultset或者还有其他更好的方法   确认用户输入?

不,您不需要直接处理JDBC Resultset,而只需要spring-security,您需要提供datasource(数据库访问详细信息)和{{1像sql这样的查询。

您可以参考here来配置JDBC身份验证。