1
votes

help me index.jsp

$("#btn-submit").click(function () {
    var username=document.getElementById("username");
    var password=document.getElementById("password");
    $.ajax({
        url:"login",
        contentType: 'application/json;charset=utf-8',
        dataType: 'text',
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        data: {
            username:username.value,
            password:password.value
        },
        type: 'get',
        success: function (response) {
            if (response=="1") {
                alert(response);
            }
            else alert(response);
        },
        error: function (x, e) {
            console.log(e)
        }
    });
});

LoginController.java

@RequestMapping("/login")
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = { "/login" }, method = RequestMethod.GET)
    @ResponseBody
    public int checkValid(@RequestParam("username") String username,@RequestParam("password") String password, HttpServletRequest request, HttpServletResponse response, Locale locale, Model model){
        try {
            if (userService.findByUserName(username).equals(hashPass(password))){
                return 1;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }
    public String hashPass(String pass) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hashInBytes = md.digest(pass.getBytes(StandardCharsets.UTF_8));

        // bytes to hex
        StringBuilder sb = new StringBuilder();
        for (byte b : hashInBytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}

spring-config-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> /WEB-INF/pages/ .jsp /resources/jdbc.properties

<!-- Enable Annotation based Declarative Transaction Management -->
<tx:annotation-driven proxy-target-class="true"
                      transaction-manager="transactionManager" />

<!-- Creating TransactionManager Bean, since JDBC we are creating of type
  DataSourceTransactionManager -->
<bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="postsDAO" class="com.blog.dao.impl.PostsDAO">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="postsService" class="com.blog.service.impl.PostsService">
    <property name="postsDAO" ref="postsDAO"/>
</bean>
<bean id="userDAO" class="com.blog.dao.impl.UserDAO">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="userService" class="com.blog.service.impl.UserService">
    <property name="userDAO" ref="userDAO"/>
</bean>

I use tomcat 9 Error:Failed to load resource: the server responded http://localhost:8080/Blog_war_exploded/login?username=root&password=root with a status of 404 ()

2

2 Answers

0
votes

Look at your error: You are accessing http://localhost:8080/Blog_war_exploded/login but you actually want to access http://localhost:8080/login.

The reason is that you specified your URL as login instead of /login, so it is relative to the current "directory" and not to the root.

Changing the code to use /login should fix it:

    $.ajax({
        url: "/login",
        ...
    })

On a side note, it's not a good idea to this via GET requests - among other things, the password will be stored in the server log in clear text. You should use a POST request instead.


Update:

Also, it seems you are use two request mappings for /login on top of each other, so you'll end up with /login/login. Check out how to use @RequestMapping properly.

Try changing the second (method-level) one to @RequestMapping(value = { "/" }, method = RequestMethod.GET) or just @RequestMapping("/").

0
votes

I think the issue is related to your RequestMapping definition on both controller level and method level.

the first login at the controller level, means if you want to access any services in this controller, your requests have to start with "/login"

@RequestMapping("/login")
@Controller
public class LoginController {

and the second login at the method level, means you want to call the /login service under /login.

 @RequestMapping(value = { "/login" }, method = RequestMethod.GET)
 @ResponseBody
 public int checkValid(@RequestParam("username") String username,@RequestParam("password") String password, HttpServletRequest request, HttpServletResponse response, Locale locale, Model model){

So the valid URL to call the /login service under /login controller is: /login/login

and because of this, your url /login was not found

you can either remove the first /login at the controller level, or use the /login/login from your ajax request...