Shiro

shiro是一个功能强大且易于使用的java安全框架,可执行身份验证、授权、加密和会话管理。

功能

Authorization

授权,即权限验证。验证某个已认证的用户是否拥有某个权限,即判断用户是否能做什么事情。常见的如:验证某个用户是否拥有某个角色;细粒度的验证某个用户对某个资源是否具有某个功能权限。

Session Management

会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通的JavaSE环境,也可以是Web环境

Cryptography

加密,保护数据的安全性,如密码加密存储到数据库,而不是铭文存储

Web Support

Web支持,可以非常容易的集成到Web环境

Caching

缓存,比如童虎登录后,其用户信息、拥有的角色/权限不必每次都去查,这样可以提高效率

Concurrency

shiro支持多线程应用的并发验证,即,如在一个县城中开启另一个线程,能把权限自动传播过去

Testing

提供测试支持

Run As

允许一个用户假装成另一个用户的身份进行访问

Remember Me

记住我,这是个非常常见的功能,即一次登录后,下次再来的话就不用登录了

三个核心组件

image.png

Subject

当前操作用户。在shiro中,Subject这一概念不仅仅指人,也可以是第三方进程,后台账户或其他类似事务。它仅仅意味着当前跟软件交互的东西

SecurityManager

是shiro框架的心脏,并作为一种保护伞对象来协调内部的安全组件共同构成一个对象图。然而,一旦SecurityManager和它的内置对象图已经配置给一个应用程序,那么它单独留下来,且应用程序开发人员几乎使用他们所有的时间来处理SubjectAPI

Realm

担当shiro和应用程序的安全数据之间的桥梁。当它实际上与安全相关的数据,如用来执行很粉验证以及授权的用户账户交互时,shiro从一个或多个为应用程序配置的Realm中寻找许多这样的东西

JAVA访问权限控制

后台请求权限控制,通过Shiro注解 @RequiresPermissions

实例

一些实体

AuthenticationInfo,用户的角色信息集合,认证时使用。

AuthorzationInfo,角色的权限信息集合,授权时使用。

DefaultWebSecurityManager,安全管理器,开发者自定义的 Realm 需要注入到 DefaultWebSecurityManager 进行管理才能生效。

ShiroFilterFactoryBean,过滤器工厂,Shiro 的基本运行机制是开发者定制规则,Shiro 去执行,具体的执行操作就是由 ShiroFilterFactoryBean 创建的一个个 Filter 对象来完成。

SimpleAuthorizationInfo()获取角色,获取权限

SimpleAuthenticationInfo()获取用户名,密码

引入pom依赖

  1. <!--引入shrio-->
  2. <dependency>
  3. <groupId>org.apache.shiro</groupId>
  4. <artifactId>shiro-spring-boot-starter</artifactId>
  5. <version>1.5.3</version>
  6. </dependency>

自定义Realm

  1. import org.apache.shiro.authc.AuthenticationException;
  2. import org.apache.shiro.authc.AuthenticationInfo;
  3. import org.apache.shiro.authc.AuthenticationToken;
  4. import org.apache.shiro.authz.AuthorizationInfo;
  5. import org.apache.shiro.realm.AuthorizingRealm;
  6. import org.apache.shiro.subject.PrincipalCollection;
  7. /**
  8. * 自定义Realm
  9. */
  10. public class CustomerRealm extends AuthorizingRealm {
  11. @Override
  12. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  13. return null;
  14. }
  15. @Override
  16. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  17. return null;
  18. }
  19. }

shiro配置

  1. @Configuration
  2. public class ShiroConfig {
  3. //ShiroFilter过滤所有请求
  4. @Bean
  5. public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
  6. ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  7. //给ShiroFilter配置安全管理器
  8. shiroFilterFactoryBean.setSecurityManager(securityManager);
  9. //配置系统受限资源
  10. //配置系统公共资源
  11. Map<String, String> map = new HashMap<String, String>();
  12. map.put("/index.jsp","authc");//表示这个资源需要认证和授权
  13. // 设置认证界面路径
  14. shiroFilterFactoryBean.setLoginUrl("/login.jsp");
  15. shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
  16. return shiroFilterFactoryBean;
  17. }
  18. //创建安全管理器
  19. @Bean
  20. public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
  21. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  22. securityManager.setRealm(realm);
  23. return securityManager;
  24. }
  25. //创建自定义Realm
  26. @Bean
  27. public Realm getRealm() {
  28. CustomerRealm realm = new CustomerRealm();
  29. return realm;
  30. }
  31. }

过滤器

shiro提供和多个默认的过滤器,可以用这些过滤器来配置控制指定url的权限

认证和退出

  1. @Controller
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @RequestMapping("logout")
  5. public String logout() {
  6. Subject subject = SecurityUtils.getSubject();
  7. subject.logout();
  8. return "redirect:/login.jsp";
  9. }
  10. @RequestMapping("/login")
  11. public String login(String username, String password) {
  12. //获取主题对象
  13. Subject subject = SecurityUtils.getSubject();
  14. try {
  15. subject.login(new UsernamePasswordToken(username,password));
  16. System.out.println("登录成功!!!");
  17. return "redirect:/index.jsp";
  18. } catch (UnknownAccountException e) {
  19. e.printStackTrace();
  20. System.out.println("用户错误!!!");
  21. } catch (IncorrectCredentialsException e) {
  22. System.out.println("密码错误!!!");
  23. }
  24. return "redirect:/login.jsp";
  25. }
  26. }

md5、salt的认证实现

  1. public class SaltUtil {
  2. public static String getSalt(int n) {
  3. char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890!@#$%^&*()".toCharArray();
  4. StringBuilder sb = new StringBuilder();
  5. for (int i = 0; i < n; i++) {
  6. char c = chars[new Random().nextInt(chars.length)];
  7. sb.append(c);
  8. }
  9. return sb.toString();
  10. }
  11. public static void main(String[] args) {
  12. System.out.println(getSalt(4));
  13. }
  14. }
  1. @Service("userService")
  2. @Transactional
  3. public class UserServiceImpl implements UserService {
  4. @Autowired
  5. private UserDao userDao;
  6. @Override
  7. public void register(User user) {
  8. //1.获取随机盐
  9. String salt = SaltUtil.getSalt(8);
  10. //2.将随机盐保存到数据
  11. user.setSalt(salt);
  12. //3.明文密码进行md5 + salt + hash散列
  13. Md5Hash MD5 = new Md5Hash(user.getPassword(),salt,1024);
  14. user.setPassword(MD5.toHex());
  15. userDao.save(user);
  16. }
  17. @Override
  18. public User findByUsername(String username) {
  19. return userDao.findByUsername(username);
  20. }
  21. }
  1. @Controller
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Autowired
  5. private UserService userService;
  6. @RequestMapping("/register")
  7. public String register(User user) {
  8. try {
  9. userService.register(user);
  10. return "redirect:/login.jsp";
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. return "redirect:/register.jsp";
  14. }
  15. }
  16. @RequestMapping("logout")
  17. public String logout() {
  18. Subject subject = SecurityUtils.getSubject();
  19. subject.logout();
  20. return "redirect:/login.jsp";
  21. }
  22. @RequestMapping("/login")
  23. public String login(String username, String password) {
  24. //获取主题对象
  25. Subject subject = SecurityUtils.getSubject();
  26. try {
  27. subject.login(new UsernamePasswordToken(username,password));
  28. System.out.println("登录成功!!!");
  29. return "redirect:/index.jsp";
  30. } catch (UnknownAccountException e) {
  31. e.printStackTrace();
  32. System.out.println("用户错误!!!");
  33. } catch (IncorrectCredentialsException e) {
  34. System.out.println("密码错误!!!");
  35. }
  36. return "redirect:/login.jsp";
  37. }
  38. }
  1. /**
  2. * 自定义Realm
  3. */
  4. public class CustomerRealm extends AuthorizingRealm {
  5. @Override
  6. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  7. return null;
  8. }
  9. @Override
  10. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  11. String principal = (String) authenticationToken.getPrincipal();
  12. //获取UserService对象
  13. UserService userService = (UserService) ApplicationContextUtil.getBean("userService");
  14. //System.out.println(userService);
  15. User user = userService.findByUsername(principal);
  16. if (!ObjectUtils.isEmpty(user)) {
  17. return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes(user.getSalt()), this.getName());
  18. }
  19. return null;
  20. }
  21. }
  1. @Component
  2. public class ApplicationContextUtil implements ApplicationContextAware {
  3. private static ApplicationContext context;
  4. @Override
  5. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  6. this.context = applicationContext;
  7. }
  8. //根据bean名字获取工厂中指定bean 对象
  9. public static Object getBean(String beanName) {
  10. return context.getBean(beanName);
  11. }
  12. }
  1. @Configuration
  2. public class ShiroConfig {
  3. //ShiroFilter过滤所有请求
  4. @Bean
  5. public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
  6. ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  7. //给ShiroFilter配置安全管理器
  8. shiroFilterFactoryBean.setSecurityManager(securityManager);
  9. //配置系统受限资源
  10. //配置系统公共资源
  11. Map<String, String> map = new HashMap<String, String>();
  12. map.put("/user/login","anon");//表示这个为公共资源 一定是在受限资源上面
  13. map.put("/user/register","anon");//表示这个为公共资源 一定是在受限资源上面
  14. map.put("/register.jsp","anon");//表示这个为公共资源 一定是在受限资源上面
  15. map.put("/**","authc");//表示这个受限资源需要认证和授权
  16. // 设置认证界面路径
  17. shiroFilterFactoryBean.setLoginUrl("/login.jsp");
  18. shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
  19. return shiroFilterFactoryBean;
  20. }
  21. //创建安全管理器
  22. @Bean
  23. public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
  24. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  25. securityManager.setRealm(realm);
  26. return securityManager;
  27. }
  28. //创建自定义Realm
  29. @Bean
  30. public Realm getRealm() {
  31. CustomerRealm realm = new CustomerRealm();
  32. HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
  33. //设置使用MD5加密算法
  34. credentialsMatcher.setHashAlgorithmName("md5");
  35. //散列次数
  36. credentialsMatcher.setHashIterations(1024);
  37. realm.setCredentialsMatcher(credentialsMatcher);
  38. return realm;
  39. }

授权实现

  1. @Override
  2. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  3. String principal = (String) principalCollection.getPrimaryPrincipal();
  4. if ("zhangsan".equals(principal)) {
  5. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  6. info.addRole("admin");
  7. info.addRole("user");
  8. info.addStringPermission("user:find:*");
  9. info.addStringPermission("admin:*");
  10. return info;
  11. }
  12. return null;
  13. }
  1. <%@page contentType="text/html;UTF-8" pageEncoding="UTF-8" isErrorPage="false" %>
  2. <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
  3. <!doctype html>
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8">
  7. <meta name="viewport"
  8. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  9. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  10. <title>Document</title>
  11. </head>
  12. <body>
  13. <%--受限资源--%>
  14. <h1>系统主页</h1>
  15. <a href="${pageContext.request.contextPath}/user/logout">退出登录</a>
  16. <ul>
  17. <shiro:hasRole name="user">
  18. <li><a href="#">用户管理</a></li>
  19. <ul>
  20. <shiro:hasPermission name="user:save:*">
  21. <li><a href="#">增加</a></li>
  22. </shiro:hasPermission>
  23. <shiro:hasPermission name="user:delete:*">
  24. <li><a href="#">删除</a></li>
  25. </shiro:hasPermission>
  26. <shiro:hasPermission name="user:update:*">
  27. <li><a href="#">修改</a></li>
  28. </shiro:hasPermission>
  29. <shiro:hasPermission name="user:find:*">
  30. <li><a href="#">查询</a></li>
  31. </shiro:hasPermission>
  32. </ul>
  33. </shiro:hasRole>
  34. <shiro:hasRole name="admin">
  35. <li><a href="#">商品管理</a></li>
  36. <li><a href="#">订单管理</a></li>
  37. <li><a href="#">物流管理</a></li>
  38. </shiro:hasRole>
  39. </ul>
  40. </body>
  41. </html>
  42. 在html中
  43. <!DOCTYPE html>
  44. <html lang="en" xmlns:th="http://www.thymeleaf.org"
  45. xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
  46. <head>
  47. <title>首页</title>
  48. </head>
  49. <body>
  50. <h1>首页</h1>
  51. <hr>
  52. <ul>
  53. <li><a href="user/index">个人中心</a></li>
  54. <li><a href="vip/index">会员中心</a></li>
  55. <p shiro:hasPermission="svip"><li>这是svip能看到的p标签</li></p>
  56. <shiro:hasPermission name="svip"><li>这是svip能看到的</li></shiro:hasPermission>
  57. <li><a href="logout">退出登录</a></li>
  58. </ul>
  59. </body>
  60. </html>
  1. @Controller
  2. @RequestMapping("order")
  3. public class OrderController {
  4. @RequestMapping("save")
  5. public String save() {
  6. //基于角色
  7. //获取主体对象
  8. Subject subject = SecurityUtils.getSubject();
  9. //代码方式
  10. if (subject.hasRole("admin")) {
  11. System.out.println("保存订单!");
  12. }else{
  13. System.out.println("无权访问!");
  14. }
  15. System.out.println("进入save方法============");
  16. return "redircet:/index.jsp";
  17. }
  18. }
  1. @Controller
  2. @RequestMapping("order")
  3. public class OrderController {
  4. @RequiresRoles(value={"admin","user"})//用来判断角色 同时具有 admin user
  5. @RequiresPermissions("user:update:01") //用来判断权限字符串
  6. @RequestMapping("save")
  7. public String save(){
  8. System.out.println("进入方法");
  9. return "redirect:/index.jsp";
  10. }
  11. }

缓存

redis+shiro

  1. import org.apache.shiro.cache.Cache;
  2. import org.apache.shiro.cache.CacheException;
  3. import org.apache.shiro.cache.CacheManager;
  4. //自定义shiro缓存管理器
  5. public class RedisCacheManager implements CacheManager {
  6. //参数1:认证或者是授权缓存的统一名称
  7. @Override
  8. public <K, V> Cache<K, V> getCache(String cacheName) throws CacheException {
  9. System.out.println(cacheName);
  10. return new RedisCache<K,V>(cacheName);
  11. }
  12. }
  1. //自定义redis缓存的实现
  2. public class RedisCache<k,v> implements Cache<k,v> {
  3. private String cacheName;
  4. public RedisCache() {
  5. }
  6. public RedisCache(String cacheName) {
  7. this.cacheName = cacheName;
  8. }
  9. @Override
  10. public v get(k k) throws CacheException {
  11. return (v) getRedisTemplate().opsForHash().get(this.cacheName, k.toString());
  12. }
  13. @Override
  14. public v put(k k, v v) throws CacheException {
  15. System.out.println("put key: "+k);
  16. System.out.println("put value:"+v);
  17. getRedisTemplate().opsForHash().put(this.cacheName,k.toString(), v);
  18. return null;
  19. }
  20. @Override
  21. public v remove(k k) throws CacheException {
  22. System.out.println("=============remove=============");
  23. return (v) getRedisTemplate().opsForHash().delete(this.cacheName,k.toString());
  24. }
  25. @Override
  26. public void clear() throws CacheException {
  27. System.out.println("=============clear==============");
  28. getRedisTemplate().delete(this.cacheName);
  29. }
  30. @Override
  31. public int size() {
  32. return getRedisTemplate().opsForHash().size(this.cacheName).intValue();
  33. }
  34. @Override
  35. public Set<k> keys() {
  36. return getRedisTemplate().opsForHash().keys(this.cacheName);
  37. }
  38. @Override
  39. public Collection<v> values() {
  40. return getRedisTemplate().opsForHash().values(this.cacheName);
  41. }
  42. private RedisTemplate getRedisTemplate(){
  43. RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtil.getBean("redisTemplate");
  44. redisTemplate.setKeySerializer(new StringRedisSerializer());
  45. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  46. return redisTemplate;
  47. }
  48. }
  1. import org.apache.shiro.codec.Base64;
  2. import org.apache.shiro.codec.CodecSupport;
  3. import org.apache.shiro.codec.Hex;
  4. import org.apache.shiro.util.ByteSource;
  5. import java.io.File;
  6. import java.io.InputStream;
  7. import java.io.Serializable;
  8. import java.util.Arrays;
  9. //自定义salt实现 实现序列化接口
  10. public class MyByteSource implements ByteSource, Serializable {
  11. private byte[] bytes;
  12. private String cachedHex;
  13. private String cachedBase64;
  14. public MyByteSource(byte[] bytes) {
  15. this.bytes = bytes;
  16. }
  17. public MyByteSource(char[] chars) {
  18. this.bytes = CodecSupport.toBytes(chars);
  19. }
  20. public MyByteSource(String string) {
  21. this.bytes = CodecSupport.toBytes(string);
  22. }
  23. public MyByteSource(ByteSource source) {
  24. this.bytes = source.getBytes();
  25. }
  26. public MyByteSource(File file) {
  27. this.bytes = (new MyByteSource.BytesHelper()).getBytes(file);
  28. }
  29. public MyByteSource(InputStream stream) {
  30. this.bytes = (new MyByteSource.BytesHelper()).getBytes(stream);
  31. }
  32. public static boolean isCompatible(Object o) {
  33. return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
  34. }
  35. public byte[] getBytes() {
  36. return this.bytes;
  37. }
  38. public boolean isEmpty() {
  39. return this.bytes == null || this.bytes.length == 0;
  40. }
  41. public String toHex() {
  42. if (this.cachedHex == null) {
  43. this.cachedHex = Hex.encodeToString(this.getBytes());
  44. }
  45. return this.cachedHex;
  46. }
  47. public String toBase64() {
  48. if (this.cachedBase64 == null) {
  49. this.cachedBase64 = Base64.encodeToString(this.getBytes());
  50. }
  51. return this.cachedBase64;
  52. }
  53. public String toString() {
  54. return this.toBase64();
  55. }
  56. public int hashCode() {
  57. return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
  58. }
  59. public boolean equals(Object o) {
  60. if (o == this) {
  61. return true;
  62. } else if (o instanceof ByteSource) {
  63. ByteSource bs = (ByteSource)o;
  64. return Arrays.equals(this.getBytes(), bs.getBytes());
  65. } else {
  66. return false;
  67. }
  68. }
  69. private static final class BytesHelper extends CodecSupport {
  70. private BytesHelper() {
  71. }
  72. public byte[] getBytes(File file) {
  73. return this.toBytes(file);
  74. }
  75. public byte[] getBytes(InputStream stream) {
  76. return this.toBytes(stream);
  77. }
  78. }
  79. }

验证码

  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.geom.AffineTransform;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.util.Arrays;
  10. import java.util.Random;
  11. /**
  12. *@创建人 cx
  13. *@创建时间 2018/11/27 17:36
  14. *@描述 验证码生成
  15. */
  16. public class VerifyCodeUtils{
  17. //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
  18. public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  19. private static Random random = new Random();
  20. /**
  21. * 使用系统默认字符源生成验证码
  22. * @param verifySize 验证码长度
  23. * @return
  24. */
  25. public static String generateVerifyCode(int verifySize){
  26. return generateVerifyCode(verifySize, VERIFY_CODES);
  27. }
  28. /**
  29. * 使用指定源生成验证码
  30. * @param verifySize 验证码长度
  31. * @param sources 验证码字符源
  32. * @return
  33. */
  34. public static String generateVerifyCode(int verifySize, String sources){
  35. if(sources == null || sources.length() == 0){
  36. sources = VERIFY_CODES;
  37. }
  38. int codesLen = sources.length();
  39. Random rand = new Random(System.currentTimeMillis());
  40. StringBuilder verifyCode = new StringBuilder(verifySize);
  41. for(int i = 0; i < verifySize; i++){
  42. verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
  43. }
  44. return verifyCode.toString();
  45. }
  46. /**
  47. * 生成随机验证码文件,并返回验证码值
  48. * @param w
  49. * @param h
  50. * @param outputFile
  51. * @param verifySize
  52. * @return
  53. * @throws IOException
  54. */
  55. public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException{
  56. String verifyCode = generateVerifyCode(verifySize);
  57. outputImage(w, h, outputFile, verifyCode);
  58. return verifyCode;
  59. }
  60. /**
  61. * 输出随机验证码图片流,并返回验证码值
  62. * @param w
  63. * @param h
  64. * @param os
  65. * @param verifySize
  66. * @return
  67. * @throws IOException
  68. */
  69. public static String outputVerifyImage(int w, int h, OutputStream os, int verifySize) throws IOException{
  70. String verifyCode = generateVerifyCode(verifySize);
  71. outputImage(w, h, os, verifyCode);
  72. return verifyCode;
  73. }
  74. /**
  75. * 生成指定验证码图像文件
  76. * @param w
  77. * @param h
  78. * @param outputFile
  79. * @param code
  80. * @throws IOException
  81. */
  82. public static void outputImage(int w, int h, File outputFile, String code) throws IOException{
  83. if(outputFile == null){
  84. return;
  85. }
  86. File dir = outputFile.getParentFile();
  87. if(!dir.exists()){
  88. dir.mkdirs();
  89. }
  90. try{
  91. outputFile.createNewFile();
  92. FileOutputStream fos = new FileOutputStream(outputFile);
  93. outputImage(w, h, fos, code);
  94. fos.close();
  95. } catch(IOException e){
  96. throw e;
  97. }
  98. }
  99. /**
  100. * 输出指定验证码图片流
  101. * @param w
  102. * @param h
  103. * @param os
  104. * @param code
  105. * @throws IOException
  106. */
  107. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException{
  108. int verifySize = code.length();
  109. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  110. Random rand = new Random();
  111. Graphics2D g2 = image.createGraphics();
  112. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  113. Color[] colors = new Color[5];
  114. Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
  115. Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
  116. Color.PINK, Color.YELLOW };
  117. float[] fractions = new float[colors.length];
  118. for(int i = 0; i < colors.length; i++){
  119. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
  120. fractions[i] = rand.nextFloat();
  121. }
  122. Arrays.sort(fractions);
  123. g2.setColor(Color.GRAY);// 设置边框色
  124. g2.fillRect(0, 0, w, h);
  125. Color c = getRandColor(200, 250);
  126. g2.setColor(c);// 设置背景色
  127. g2.fillRect(0, 2, w, h-4);
  128. //绘制干扰线
  129. Random random = new Random();
  130. g2.setColor(getRandColor(160, 200));// 设置线条的颜色
  131. for (int i = 0; i < 20; i++) {
  132. int x = random.nextInt(w - 1);
  133. int y = random.nextInt(h - 1);
  134. int xl = random.nextInt(6) + 1;
  135. int yl = random.nextInt(12) + 1;
  136. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
  137. }
  138. // 添加噪点
  139. float yawpRate = 0.05f;// 噪声率
  140. int area = (int) (yawpRate * w * h);
  141. for (int i = 0; i < area; i++) {
  142. int x = random.nextInt(w);
  143. int y = random.nextInt(h);
  144. int rgb = getRandomIntColor();
  145. image.setRGB(x, y, rgb);
  146. }
  147. shear(g2, w, h, c);// 使图片扭曲
  148. g2.setColor(getRandColor(100, 160));
  149. int fontSize = h-4;
  150. Font font = new Font("Algerian", Font.ITALIC, fontSize);
  151. g2.setFont(font);
  152. char[] chars = code.toCharArray();
  153. for(int i = 0; i < verifySize; i++){
  154. AffineTransform affine = new AffineTransform();
  155. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);
  156. g2.setTransform(affine);
  157. g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);
  158. }
  159. g2.dispose();
  160. ImageIO.write(image, "jpg", os);
  161. }
  162. private static Color getRandColor(int fc, int bc) {
  163. if (fc > 255)
  164. fc = 255;
  165. if (bc > 255)
  166. bc = 255;
  167. int r = fc + random.nextInt(bc - fc);
  168. int g = fc + random.nextInt(bc - fc);
  169. int b = fc + random.nextInt(bc - fc);
  170. return new Color(r, g, b);
  171. }
  172. private static int getRandomIntColor() {
  173. int[] rgb = getRandomRgb();
  174. int color = 0;
  175. for (int c : rgb) {
  176. color = color << 8;
  177. color = color | c;
  178. }
  179. return color;
  180. }
  181. private static int[] getRandomRgb() {
  182. int[] rgb = new int[3];
  183. for (int i = 0; i < 3; i++) {
  184. rgb[i] = random.nextInt(255);
  185. }
  186. return rgb;
  187. }
  188. private static void shear(Graphics g, int w1, int h1, Color color) {
  189. shearX(g, w1, h1, color);
  190. shearY(g, w1, h1, color);
  191. }
  192. private static void shearX(Graphics g, int w1, int h1, Color color) {
  193. int period = random.nextInt(2);
  194. boolean borderGap = true;
  195. int frames = 1;
  196. int phase = random.nextInt(2);
  197. for (int i = 0; i < h1; i++) {
  198. double d = (double) (period >> 1)
  199. * Math.sin((double) i / (double) period
  200. + (6.2831853071795862D * (double) phase)
  201. / (double) frames);
  202. g.copyArea(0, i, w1, 1, (int) d, 0);
  203. if (borderGap) {
  204. g.setColor(color);
  205. g.drawLine((int) d, i, 0, i);
  206. g.drawLine((int) d + w1, i, w1, i);
  207. }
  208. }
  209. }
  210. private static void shearY(Graphics g, int w1, int h1, Color color) {
  211. int period = random.nextInt(40) + 10; // 50;
  212. boolean borderGap = true;
  213. int frames = 20;
  214. int phase = 7;
  215. for (int i = 0; i < w1; i++) {
  216. double d = (double) (period >> 1)
  217. * Math.sin((double) i / (double) period
  218. + (6.2831853071795862D * (double) phase)
  219. / (double) frames);
  220. g.copyArea(i, 0, 1, h1, 0, (int) d);
  221. if (borderGap) {
  222. g.setColor(color);
  223. g.drawLine(i, (int) d, i, 0);
  224. g.drawLine(i, (int) d + h1, i, h1);
  225. }
  226. }
  227. }
  228. public static void main(String[] args) throws IOException {
  229. //获取验证码
  230. String s = generateVerifyCode(4);
  231. //将验证码放入图片中
  232. outputImage(260,60,new File("/Users/chenyannan/Desktop/安工资料/aa.jpg"),s);
  233. System.out.println(s);
  234. }
  235. }
  1. @RequestMapping("getImage")
  2. public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
  3. //生成验证码
  4. String code = VerifyCodeUtils.generateVerifyCode(4);
  5. //验证码放入session
  6. session.setAttribute("code",code);
  7. //验证码存入图片
  8. ServletOutputStream os = response.getOutputStream();
  9. response.setContentType("image/png");
  10. VerifyCodeUtils.outputImage(220,60,os,code);
  11. }
  1. @RequestMapping("login")
  2. public String login(String username, String password,String code,HttpSession session) {
  3. //比较验证码
  4. String codes = (String) session.getAttribute("code");
  5. try {
  6. if (codes.equalsIgnoreCase(code)){
  7. //获取主体对象
  8. Subject subject = SecurityUtils.getSubject();
  9. subject.login(new UsernamePasswordToken(username, password));
  10. return "redirect:/index.jsp";
  11. }else{
  12. throw new RuntimeException("验证码错误!");
  13. }
  14. } catch (UnknownAccountException e) {
  15. e.printStackTrace();
  16. System.out.println("用户名错误!");
  17. } catch (IncorrectCredentialsException e) {
  18. e.printStackTrace();
  19. System.out.println("密码错误!");
  20. }catch (Exception e){
  21. e.printStackTrace();
  22. System.out.println(e.getMessage());
  23. }
  24. return "redirect:/login.jsp";
  25. }