spring boot+thymeleaf+ajax实现登录验证

pom依赖我就不列出来了,太长了

  1. controller层
@RequestMapping("/checklogin")
	@ApiOperation("登录校验") // 注解:应用在 controller 里面的方法上,用于swagger
	@ResponseBody
	public Map<String, Object> checklogin(@Param("username") String username, @Param("password") String password,
			HttpSession session) throws Exception {
		Map<String,Object>map=new HashMap<String,Object>();
		if (userServiceImpl.checklogin(username, password) > 0) {
			User user = new User();
			user.setPassword(password);
			user.setUsername(username);
			session.setAttribute("USER_SESSION", user);
			session.setMaxInactiveInterval(180);// session失效时间为三分,即180s
			map.put("code",1);
			return map;// redirect
		} else {
			map.put("code", 0);
			return map;
		}

	};

一定要加上responsebody,不然会被thymeleaf引擎渲染为html页面
一定要加上responsebody,不然会被thymeleaf引擎渲染为html页面
一定要加上responsebody,不然会被thymeleaf引擎渲染为html页面

  1. serviceimpl类:
	/**
	 * 登录校验
	 */
	public Integer checklogin(String username, String password){
		return userMapper.checklogin(username, password);
	}
  1. mapper接口类:
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {


	@Select("SELECT IFNULL( count( NAME ), 0 ) AS NAME FROM USER WHERE NAME = #{username} AND PASSWORD = #{password}")
	Integer checklogin(@Param("username") String username, @Param("password") String password);
}

  1. application.properties:
#mysql 
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myblog?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
##########Alibaba.Druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=2
spring.datasource.minIdle=2
spring.datasource.maxActive=3
spring.datasource.maxWait=6000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.connectionInitSqls=set names utf8mb4
#####Port
server.port=8088
##XiangMuMing
server.servlet.context-path=/blog
PROJECT_NAME=/blog
#####DEBUG
logging.level.com.hxz.mapper.*=debug
#########################################
spring.banner.charset=UTF-8
spring.messages.encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
###########Redisache
###Themleaf
spring.thymeleaf.prefix=classpath\:/templates/  
spring.thymeleaf.check-template-location=true  
spring.thymeleaf.suffix=.html  
spring.thymeleaf.encoding=UTF-8  
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=true 
# set to false for hot refresh
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
#####redis
spring.redis.database=0  
spring.redis.host=localhost
spring.redis.port=6379  
spring.redis.password=
spring.redis.pool.max-active=8  
spring.redis.pool.max-wait=-1  
spring.redis.pool.max-idle=8  
spring.redis.pool.min-idle=0  
spring.redis.timeout=50000
# pagehelper properties
pagehelper.offsetAsPageNum=true
pagehelper.rowBoundsWithCount=true
pagehelper.pageSizeZero=true
pagehelper.reasonable=false
pagehelper.params=pageNum=pageHelperStart;pageSize=pageHelperRows;
pagehelper.supportMethodsArguments=false
  1. ajax部分:
<script type="text/javascript">
	// onclick触发js
	function login() {
		var username = $.trim($("#username").val());
		var password = $.trim($("#password").val());
		if (username == "") {
			alert("请输入用户名");
			return false;
		} else if (password == "") {
			alert("请输入密码");
			return false;
		}
		//ajax去服务器端校验
		var data = {
			username : username,
			password : password
		};
		alert(data);
		$.ajax({
			type : 'POST',
			url : '/blog/admin/checklogin',
			data : data,
			dataType : 'json',
			success : function(data) {
				if (data == null) {
					alert("没有这个用户!")
				} else {
					window.location.href = "/blog/admin/index";

				}
			},
		});
	}
	;
</script>

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值