文章目录
Springboot
1、实体类
/*
@PropertySource :加载指定的配置文件;
我们去在resources目录下新建一个person.properties文件
//@PropertySource(value = "classpath:person.properties")
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
@configurationProperties:默认从全局配置文件中获取值;
*/
@Component //注册bean
@ConfigurationProperties(prefix = "person")
//@PropertySource(value = "classpath:person.properties")
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
实体类在类中赋值
//直接使用@value
@Value("${user.name}") //从配置文件中取值
private String name;
@Value("#{9*2}") // #{SPEL} Spring表达式
private int age;
@Value("男") // 字面量
private String sex;
用配置文件.yml赋值
person:
name: qinjiang${random.uuid} # 随机uuid
age: ${random.int} # 随机int
happy: false
birth: 2000/01/01
maps: {k1: v1,k2: v2}
lists:
- code
- girl
- music
dog:
name: ${person.hello:other}_旺财
age: 1
2、多环境配置
1、多个环境配置文件
spring:
profiles:
active: dev
2、一个配置文件用—来区分不同环境
3、访问静态资源
再项目根目录下或者在resources资源根目录下创建resources/static/public文件再创建静态资源即可访问
自定义静态资源路径
在application.properties中配置;
spring.resources.static-locations=classpath:/coding/,classpath:/kuang/
小结:
- 在SpringBoot,我们可以使用以下方式处理静态资源
- webjars
localhost:8080/webjarts/
- public,static,/**,resources
localhost:8080/
- webjars
- 优先级:resources>static(默认)>public
4、Thymeleaf
1、导入依赖
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
2、在resources下创建templates文件
3、写html
4、写controller
- 在templates目录下的所有页面之恶能通过controller来跳转!
5、Shiro
Apache Shiro是一个java的安全(权限)框架,提供了认证、授权、加密和会话管理等功能 。
认证:验证用户的身份
授权:对用户执行访问控制:判断用户是否被允许做某事
管理:在任何环境下使用 Session API,即使没有 Web 或EJB 容器。
加密:以更简洁易用的方式使用加密功能,保护或隐藏数据防止被偷窥
Realms:聚集一个或多个用户安全数据的数据源
单点登录(SSO)功能:为没有关联到登录的用户启用 "Remember Me“ 服务
6.Spring集成shiro
1.在pom.xml文件中引入对Shiro的依赖
<!-- shiro 包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-quartz</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.2</version>
</dependency>
2.在config目录下创建Shiro配置文件spring-shiro-web.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userShiroRealm"/>
</bean>
<!-- 自定义域 -->
<bean id="userShiroRealm" class="com.bug.realm.UserShiroRealm">
<property name="userService" ref="userService"/>
<property name="credentialsMatcher" ref="credentialsMatcher"/>
<property name="cachingEnabled" value="true"/>
</bean>
<!-- 自定义凭证(密码)匹配器 -->
<bean id="credentialsMatcher" class="com.bug.credentials.BugCredentialsMatcher"></bean>
<!-- 自定义登录验证过滤器 -->
<bean id="loginCheckPermissionFilter" class="com.bug.filter.LoginCheckPermissionFilter"></bean>
<!-- Shiro的web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"></property>
<property name="unauthorizedUrl" value="/index.jsp"></property>
<property name="filters">
<map>
<entry key="authc" value-ref="loginCheckPermissionFilter"></entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/index.jsp = anon
/unauthorized.jsp = anon
/user/checkLogin = anon
/user/queryUserInfo = authc
/user/logout = anon
/pubApi/** = anon
</value>
</property>
</bean>
</beans>