编写Controller中的登录方法:
@RequestMapping("/login1") public String Login(String username,String password,Model model){ // 获取当前的用户 Subject subject = SecurityUtils.getSubject(); // 封装用户的登入数据 ----> 封装成为令牌进行加密 UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { // 如果执行成功 subject.login(token); //执行登入的方法,如果没有异常就进行登入 return "index"; } catch (UnknownAccountException e) { //用户名不存在 model.addAttribute("msg","用户名错误"); System.out.println("用户名错误"); return "user/Login"; }catch (IncorrectCredentialsException e) { //密码不存在 System.out.println("密码错误"); model.addAttribute("msg","密码错误"); return "user/Login"; } }
(4).Shiro整合Mybatis
1.首先导入需要的所有的mavem依赖
①mysql-connect ②druid ③log4j ④mybtis-spring-boot-starter
<!-- JDBC驱动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--数据库驱动--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!--德鲁伊--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!-- log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- Mybatis 驱动包--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
2.编写配置文件application.properties或者application.yaml
application.Properties
# properties 这个文件负责对数据起别名和指向mapper.xml mybatis.type-aliases-package=com.jsxs.pojo mybatis.mapper-locations=classpath:mapper/*.xml
application.yaml
# yaml 负责编写数据源 spring: datasource: # 我们这里用户名和密码都不需要双引号 , username: root password: 121788 url: jdbc:mysql://localhost:3306/mybatis?userUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver # 我们这里指定的是阿里巴巴的 德鲁伊 的数据源 type: com.alibaba.druid.pool.DruidDataSource
3.编写jsxs文件下的pojo,controller,service,mapper文件夹参考前面的springboot整合mybatis代码哦!
整合完毕的文件目录:
在mapper中添加了一个queryUserByName的方法:
package com.jsxs.mapper; import com.jsxs.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Repository @Mapper public interface UserMapper { // 通过用户名查找用户 public User queryByName(String name); }
4.编写根目录下mapper/mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 1. 我们首先要绑定工作空间--> <mapper namespace="com.jsxs.mapper.UserMapper"> <!-- 2. 我们编写SQL语句 --> <!-- id是方法名, 返回类型是实体类 --> <select id="queryByName" parameterType="String" resultType="com.jsxs.pojo.User"> select *from user where name=#{name} </select> </mapper>
5.创建service层 并且编写UserService.java接口以及接口的实现类 UserServiceImpl.java
package com.jsxs.service; import com.jsxs.pojo.User; public interface UserService { public User queryByName(String name); }
package com.jsxs.service; import com.jsxs.mapper.UserMapper; import com.jsxs.pojo.User; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserServiceImpl implements UserService{ @Resource private UserMapper userMapper; @Override public User queryByName(String name) { return userMapper.queryByName(name); } }
6.进行测试数据
测试成功
7.修改成数据库信息
UserRealm.java
修改
package com.jsxs.config; import com.jsxs.pojo.User; import com.jsxs.service.UserService; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.Subject; import javax.annotation.Resource; public class UserRealm extends AuthorizingRealm { // 授权 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { System.out.println("执行了=》授权的方法"); return null; } //我们需要添加业务层的支持 @Resource UserService userService; // 认证 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("执行了=》认证的方法"); // 链接虚拟的数据库 // String name = "admin"; // String password = "123456"; // 链接真实的数据库 UsernamePasswordToken userToken = (UsernamePasswordToken) token; User user = userService.queryByName(userToken.getUsername()); //获取前端的名字,通过前端的截取的用户名进行查询 if (user==null) { // 假如说查询的user为空,那么就返回null return null; // 抛出异常 UnknownAccount } // 密码认证 ----》Shiro帮我们做 return new SimpleAuthenticationInfo("",user.getPwd(),""); } }
ShiroConfig
无修改
package com.jsxs.config; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.LinkedHashMap; import java.util.Map; @Configuration public class ShiroConfig { // ShiroFilterFactoryBean ----》 Subject @Bean(name = "shiroFilterFactoryBean") public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){ // 通过注解指定是 public DefaultWebSecurityManager getDefaultWebSecurityManager 这个Bean ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager); //设置安全管理器 /** * anno :无需认证就可以访问 * authc : 必须认证了才能访问 * user :必须拥有 记住我 功能才有用 * perms : 拥有对某个资源的权限才能访问 * role : 拥有某个角色权限次啊能使用 */ Map<String, String> filterMap = new LinkedHashMap<>();// filterMap.put("/add","authc"); filterMap.put("/add.html","authc"); filterMap.put("/update","authc"); filterMap.put("/update.html","authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap); //这个一定要添加 //设置登入的请求 shiroFilterFactoryBean.setLoginUrl("/toLogin"); return shiroFilterFactoryBean; } //DefaultWebSecurityManger ----》 SecurityManger ---->02 @Bean public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){ // 通过注解指定是 public UserRealm userRealm() 这个Bean DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(); defaultWebSecurityManager.setRealm(userRealm); //进行关联 Realm return defaultWebSecurityManager; } //创建 Realm 对象 ----》 Realm 需要自定义----> 01 @Bean(name = "userRealm") public UserRealm userRealm(){ return new UserRealm(); } }
测试一下bingo搞定