week 7.6-7.12
- | Study-update |
---|---|
-Mon | 使用springboot的JdbcTemplate进行增删改查 |
-Tue | springboot整合Mybatis |
-Wes | springSecurity |
- | - |
-Fri | SpringSecurity控制权限显示相应东西 |
- | - |
- | Study-update |
-Sun | ssm配置thymeleaf |
7.6 Monday
引入starter jdbc mysql-connector
配置application.yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/webtest?serviceTimeZone=UTC&useUnicode=true&characterEncoding=utf-8
package com.maaoooo.springbootdemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* @author lzr
* @date 2020/7/6 14:14:58
* @description
*/
@RestController
public class JdbcController {
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping("/test")
public List<Map<String, Object>> test() {
String sql = "select * from user";
List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
return mapList;
}
@RequestMapping("/addUser")
public String addUser() {
String sql = "insert into user(username,password) values('hello','123123123')";
jdbcTemplate.update(sql);
return "updateOK";
}
@RequestMapping("/update/{id}")
public String update(@PathVariable("id") int id) {
String sql = "update user set username=?,password=? where id=" + id;
Object[] objects = new Object[2];
objects[0] = "小明";
objects[1] = "123123123123";
jdbcTemplate.update(sql, objects);
return "updateOK";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable int id) {
String sql = "delete from user where id=" + id;
jdbcTemplate.update(sql);
return "deleteSuccess";
}
}
7.7 Tuesday
导入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
配置文件
mybatis.type-aliases-package=com.maaoooo.springbootmybatis.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
7.8 Wednesday
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Config
/**
* @author lzr
* @date 2020/7/9 11:36:09
* @description
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("xiaoliu").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
.and().withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
}
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应权限才能访问
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认转跳
http.formLogin();
http.logout().logoutSuccessUrl("/index");
}
}
thymeleaf控制什么权限显示什么页面
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<!--如果未登录-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<!-- 如果登录 显示注销和账户名-->
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="sign out icon"></i> 注销
</a>
</div>
<div sec:authorize="isAuthenticated()">
用户名:<span sec:authentication="name"></span>
</div>
7.10 Friday
sec:authorize=“haseRole(‘role’)”
<div class="ui three column stackable grid" >
<div class="column" sec:authorize="hasRole('vip1')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 1</h5>
<hr>
<div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
<div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
<div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
</div>
</div>
</div>
</div>
<div class="column" sec:authorize="hasRole('vip2')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 2</h5>
<hr>
<div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
<div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
<div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
</div>
</div>
</div>
</div>
<div class="column" sec:authorize="hasRole('vip3')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 3</h5>
<hr>
<div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
<div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
<div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
</div>
</div>
</div>
</div>
</div>
记住我 登录页自定义
/**
* @author lzr
* @date 2020/7/9 11:36:09
* @description
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("xiaoliu").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2")
.and().withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
.and().withUser("1").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应权限才能访问
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认转跳 自定义页面
http.formLogin().loginPage("/login").loginProcessingUrl("/login").passwordParameter("password");
//注销后转跳
http.logout().logoutSuccessUrl("/index");
//关闭csrf
http.csrf().disable();
//记住我功能cookie 默认保存两周
http.rememberMe().rememberMeParameter("remember");
}
}
7.12 Friday
ssm配置thymeleaf
导入依赖
5会导致无法使用el表达式
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
spring-mvc配置
<!-- 模板解析器 -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean>