Skip to content

Commit 10696b7

Browse files
committedSep 20, 2017
add redis
1 parent acc0689 commit 10696b7

File tree

11 files changed

+324
-13
lines changed

11 files changed

+324
-13
lines changed
 

‎build.gradle

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,16 @@ subprojects{
9797
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.2.0'
9898
//表现层模板配置
9999
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
100-
//Security配置
100+
//SpringSecurity配置
101101
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'
102102
//消息中间件MQ
103103
compile ('org.springframework.boot:spring-boot-starter-activemq')
104104
//WEB容器配置
105105
// compile("org.springframework.boot:spring-boot-starter-jetty")
106106

107+
//redis数据库配置
108+
compile('org.springframework.boot:spring-boot-starter-data-redis')
109+
107110
/*# ===================================================================
108111
# SPRING BOOT GRADLE 普通配置类
109112
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ziniu.controller;
2+
3+
import com.ziniu.domain.PersonDemo;
4+
import com.ziniu.repository.PersonRedisDao;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
10+
@RestController
11+
public class DataControllerForRedis {
12+
13+
@Autowired
14+
PersonRedisDao personRedisDao;
15+
16+
@RequestMapping("/set") //1
17+
public void set(){
18+
PersonDemo person = new PersonDemo("2","dingjiagong", 27);
19+
personRedisDao.save(person);
20+
personRedisDao.stringRedisTemplateDemo();
21+
}
22+
23+
@RequestMapping("/getStr") //2
24+
public String getStr(){
25+
return personRedisDao.getString();
26+
}
27+
28+
@RequestMapping("/getPerson") //3
29+
public PersonDemo getPerson(){
30+
return personRedisDao.getPerson();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.ziniu.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import javax.servlet.ServletOutputStream;
8+
import javax.servlet.http.HttpServletResponse;
9+
import java.io.BufferedInputStream;
10+
import java.io.FileInputStream;
11+
import java.io.FileNotFoundException;
12+
import java.io.IOException;
13+
14+
/**
15+
* Created by yeoman on 2017/7/27.
16+
*/
17+
@RestController
18+
public class ShowPdfController {
19+
20+
private static String[] cidArr = {"D:/cto培训/规划能力番外篇@菡萏如佳人.pdf",
21+
"D:/cto培训/什么才是好的规划@菡萏如佳人.pdf",
22+
"D:/cto培训/CTO内训营—技术能力篇@菡萏如佳人.pdf",
23+
"D:/cto培训/CTO内训营—规划能力篇@菡萏如佳人.pdf",
24+
"D:/cto培训/CTO内训营—认知能力篇@菡萏如佳人.pdf",
25+
"D:/cto培训/CTO内训营—学习能力篇@菡萏如佳人.pdf",
26+
"D:/cto培训/CTO内训营—沟通能力篇@菡萏如佳人.pdf",
27+
"D:/cto培训/CTO内训营—领导能力篇@菡萏如佳人.pdf"};
28+
29+
@GetMapping("/showPdf/{id}")
30+
public void showPdf(@PathVariable Integer id, HttpServletResponse response){
31+
BufferedInputStream bis = null;
32+
try {
33+
id--;
34+
bis = new BufferedInputStream(new FileInputStream(cidArr[id]));
35+
response.setCharacterEncoding("utf-8");
36+
response.setContentType("application/pdf");
37+
response.setHeader("X-Frame-Options", "SAMEORIGIN");
38+
ServletOutputStream os = response.getOutputStream();
39+
byte[] bytes = new byte[1024*1024];
40+
int len;
41+
while ((len=bis.read(bytes)) != -1){
42+
os.write(bytes, 0, len);
43+
}
44+
os.flush();
45+
os.close();
46+
os = null;
47+
bis.close();
48+
bis = null;
49+
} catch (FileNotFoundException e) {
50+
e.printStackTrace();
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.ziniu.domain;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Copyright © 2016年 author. All rights reserved.
7+
*
8+
* @Author 临江仙 hzqiuxm@163.com
9+
* @Date 2017/9/2 0002 20:52
10+
*/
11+
public class PersonDemo implements Serializable {
12+
13+
private static final long serialVersionUID = 1L;
14+
15+
private String id;
16+
private String name;
17+
private Integer age;
18+
19+
public PersonDemo() {
20+
super();
21+
}
22+
23+
public PersonDemo(String id, String name, Integer age) {
24+
super();
25+
this.id = id;
26+
this.name = name;
27+
this.age = age;
28+
}
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public Integer getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(Integer age) {
51+
this.age = age;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.ziniu.repository;
2+
3+
import com.ziniu.domain.PersonDemo;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.redis.core.RedisTemplate;
6+
import org.springframework.data.redis.core.StringRedisTemplate;
7+
import org.springframework.data.redis.core.ValueOperations;
8+
import org.springframework.stereotype.Repository;
9+
10+
import javax.annotation.Resource;
11+
12+
/**
13+
* Copyright © 2016年 author. All rights reserved.
14+
*
15+
* @Author 临江仙 hzqiuxm@163.com
16+
* @Date 2017/9/12 0012 14:51
17+
* Redis的数据库访问层
18+
*/
19+
@Repository
20+
public class PersonRedisDao {
21+
22+
@Autowired
23+
StringRedisTemplate stringRedisTemplate;
24+
25+
@Resource(name="stringRedisTemplate")
26+
ValueOperations<String,String> valueOpsStr;
27+
28+
@Autowired
29+
RedisTemplate<Object,Object> redisTemplate;
30+
31+
@Resource(name="redisTemplate")
32+
ValueOperations<Object,Object> valOps;
33+
34+
public void stringRedisTemplateDemo(){
35+
valueOpsStr.set("xx","yy");
36+
}
37+
38+
public void save(PersonDemo personDemo){
39+
valOps.set(personDemo.getId(),personDemo);
40+
}
41+
42+
public String getString(){
43+
return valueOpsStr.get("xx");
44+
}
45+
46+
public PersonDemo getPerson(){
47+
return (PersonDemo) valOps.get("1");
48+
}
49+
50+
}

‎service/src/main/java/com/ziniu/service/Impl/HelloServcice.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.jsonwebtoken.Jwts;
44
import io.jsonwebtoken.SignatureAlgorithm;
55
import io.jsonwebtoken.impl.crypto.MacProvider;
6-
import org.junit.jupiter.api.Test;
76
import org.springframework.stereotype.Service;
87

98
import java.security.Key;
@@ -32,6 +31,10 @@ public String sayHello(){
3231
public static void main(String[] args) {
3332

3433
Key key = MacProvider.generateKey();
34+
35+
System.out.println("key: " + key);
36+
37+
3538
String compactJws = Jwts.builder().setSubject("hzqiuxm")
3639
.signWith(SignatureAlgorithm.HS512, key)
3740
.setExpiration(new Date(System.currentTimeMillis()+300000))
@@ -40,7 +43,9 @@ public static void main(String[] args) {
4043

4144
System.out.println("compactJws: " + compactJws);
4245

43-
MacProvider.generateKey();
46+
// MacProvider.generateKey();
47+
if(Jwts.parser().setSigningKey(key).parseClaimsJws(compactJws).getBody().getSubject().equals("hzqiuxm"))
48+
System.out.println("is ok!");
4449

4550

4651
}
@@ -56,7 +61,6 @@ public static void main(String[] args) {
5661
// }
5762

5863

59-
@Test
6064
public final void testJws(){
6165

6266
Key key = MacProvider.generateKey();

‎web/src/main/java/com/ziniu/Application.java

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
package com.ziniu;
22

3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.PropertyAccessor;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
36
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
47
import org.apache.activemq.command.ActiveMQQueue;
58
import org.springframework.boot.SpringApplication;
69
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
711
import org.springframework.boot.web.servlet.ServletComponentScan;
812
import org.springframework.context.annotation.Bean;
913
import org.springframework.context.annotation.ComponentScan;
1014
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.data.redis.connection.RedisConnectionFactory;
16+
import org.springframework.data.redis.core.RedisTemplate;
17+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
18+
import org.springframework.data.redis.serializer.StringRedisSerializer;
1119

1220

1321
import javax.jms.Queue;
22+
import java.net.UnknownHostException;
1423

1524
/**
1625
* Copyright © 2016年 author. All rights reserved.
1726
*
1827
* @Author 临江仙 hxqiuxm@163.com
1928
* @Date 2017/3/1 0001 10:39
2029
*/
21-
@Configuration
22-
@EnableAutoConfiguration
23-
@ComponentScan(basePackages = {"com.ziniu"})
30+
//
31+
@SpringBootApplication
2432
@EnableEncryptableProperties
2533
@ServletComponentScan
2634
public class Application {
@@ -31,7 +39,25 @@ public Queue queue() {
3139
return new ActiveMQQueue("sample.queue");
3240
}
3341

42+
@Bean
43+
@SuppressWarnings({ "rawtypes", "unchecked" })
44+
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
45+
throws UnknownHostException {
46+
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
47+
template.setConnectionFactory(redisConnectionFactory);
48+
49+
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
50+
ObjectMapper om = new ObjectMapper();
51+
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
52+
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
53+
jackson2JsonRedisSerializer.setObjectMapper(om);
3454

55+
template.setValueSerializer(jackson2JsonRedisSerializer); //1 值采用jackson2JsonRedisSerializer序列化
56+
template.setKeySerializer(new StringRedisSerializer()); //2 键采用StringRedisSerializer序列化
57+
58+
template.afterPropertiesSet();
59+
return template;
60+
}
3561

3662
public static void main(String[] args) {
3763

‎web/src/main/java/com/ziniu/config/WebSecurityConfig.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,41 @@ ZnAuthenticationProvider znAuthenticationProvider(){
4444
// }
4545

4646

47-
47+
/**
48+
* 认证信息管理
49+
* @param auth
50+
* @throws Exception
51+
*/
4852
@Override
4953
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
5054
// auth.userDetailsService(customUserService());//自动验证
5155
auth.authenticationProvider(znAuthenticationProvider());//自定义认证
56+
// auth.inMemoryAuthentication()
57+
// .withUser("qiuxm").password("123456").roles("admin"); //认证信息存储在内存中
5258

5359

5460

5561
}
5662

5763

58-
64+
/**
65+
* 安全认证配置
66+
* @param http
67+
* @throws Exception
68+
*/
5969
@Override
6070
protected void configure(HttpSecurity http) throws Exception {
6171
http.authorizeRequests()
62-
.antMatchers("/app/commons/**","/img/**","/qiuxm/**","/durid/**").permitAll()
72+
.antMatchers("/app/commons/**","/img/**","/qiuxm/**","/durid/**","/**").permitAll()
6373
.anyRequest().authenticated() //4
6474
.and()
65-
.formLogin()
66-
.loginPage("/login")
75+
.formLogin() //基于Form表单登录验证
76+
.loginPage("/login") //自定义登录页面
6777
.failureUrl("/login?error")
6878
.permitAll() //5
6979
.and()
7080
.logout().permitAll(); //6
81+
http.headers().frameOptions().disable();
7182

7283
//使用的是JWT,我们这里不需要csrf
7384
http.csrf().disable().sessionManagement();
@@ -77,4 +88,6 @@ protected void configure(HttpSecurity http) throws Exception {
7788

7889

7990

91+
92+
8093
}

0 commit comments

Comments
 (0)