SpringMVC之安全性(二)登录界面
来源:互联网 发布:淘宝可以买限定皮肤吗 编辑:程序博客网 时间:2024/06/02 07:30
在这一节,我们将把之前的SeurityConfiguration的类分成下面两个:
1) ApiSecurityConfiguration :这个是首先被布局的。这个确保使用基本的身份验证RESTful。
2) WebSecurityConfiguration :这个是布局重置登录的页面。
1. ApiSecurityConfiguration
我们一样的删除了之前的SeurityConfiguration,然后在相同的包下面添加ApiSecurityConfiguration类,加入的代码如下 :
package masterSpringMVC6.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import org.springframework.http.HttpMethod;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/** * 基础权限的设定,关于用户的权限 * Created by OwenWilliam on 2016/5/21. */@Configuration@Order(1)//最先执行public class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter{ /** * 这个是数据库提取相匹配的。 * 这里我们规定是固定的用户名和密码 * @param auth * @throws Exception */ @Autowired public void configureAuth(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("user").password("user").roles("USER").and() .withUser("admin").password("admin").roles("USER", "ADMIN"); } /** * 规定什么样的角色可以有什么样的操作 * 如,User的只能查看,admin有CRUD * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { http .antMatcher("/api/**") .httpBasic().and() .csrf().disable() .authorizeRequests() .antMatchers(HttpMethod.GET).hasRole("USER") .antMatchers(HttpMethod.POST).hasRole("ADMIN") .antMatchers(HttpMethod.PUT).hasRole("ADMIN") .antMatchers(HttpMethod.DELETE).hasRole("ADMIN") .anyRequest().authenticated(); }}
2. WebSecurityConfiguration
在相同的包下添加,然后加下的代码如下:
package masterSpringMVC6.config;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/** * 登录的权限,像URI的地址 * Created by OwenWilliam on 2016/5/21. */@Configurationpublic class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http .formLogin() .loginPage("/login") .defaultSuccessUrl("/profile") .and() .logout().logoutSuccessUrl("/login") .and() .authorizeRequests() .antMatchers("/webjars/**", "/login", "/signin/**", "/signup").permitAll() .anyRequest().authenticated(); }}
上面的代码就已经规格了登录的页面的重置到login的视图页面。所以我们还需要加入个视图页面。在加入视图页面时,我们需要定义个控制器,让控制器来调用视图。
在authentication的包下加入LoginController的类。
package masterSpringMVC6.authentication;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * 用户普通登录 * Created by OwenWilliam on 2016/5/21. */@Controllerpublic class LoginController { @RequestMapping("/login") public String authenticate() { return "login"; }}
3.视图界面
好了,我们现在添加个login.html.
<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout/default"><head> <title>Login</title></head><body><div class="section no-pad-bot" layout:fragment="content"> <div class="container"> <h2 class="header center orange-text">Login</h2> <div class="row"> <div id="errorMessage" class="card-panel red lighten-2" th:if="${param.error}"> <span class="card-title">Invalid user name or password</span> </div> <form class="col s12" action="/login" method="post"> <div class="row"> <div class="input-field col s12"> <input id="username" name="username" type="text" class="validate"/> <label for="username">Username</label> </div> </div> <div class="row"> <div class="input-field col s12"> <input id="password" name="password" type="password" class="validate"/> <label for="password">Password</label> </div> </div> <div class="row center"> <button class="btn indigo waves-effect waves-light" type="submit" name="action">Submit <i class="mdi-content-send right"></i> </button> </div> <!--伪造自己的信息,使其可信用,去跨站访问--> <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/> </form> </div> </div></div></body></html>
4.总结
这章节的学习,我们主要是优化上一节的内容,我们自己定义一个登录的页面,这个页面更适合我们的系统。同时,我们将SeurityConfiguration给分割成了两个,可以说ApiSeurityConfiguration是API用的,而WebSeurityConfiguration是我们访问网站时的页面跳转。最后,我们登录的页面如下。
源码下载:git@github.com:owenwilliam/masterSpringMVC.git
0 0
- SpringMVC之安全性(二)登录界面
- SpringMVC学习之用户登录(二)
- SpringMVC之安全性(一)
- Qt之QQ登录界面(二)
- 仿美团(二)之登录界面
- SpringMVC之安全性(三)Twitter登入
- springMVC用户登录实例(二)
- CSS 实现 系统登录界面 (二)
- 界面编程之登录界面
- Qt之QQ登录界面(一)
- Qt之QQ登录界面(三)
- Android之高仿微信“登录界面”(三)
- UI之登录界面
- ServletJsp之登录界面
- Android 之登录界面
- Android之登录界面
- HTML之登录界面
- 简单的web登录界面加入Hibernate(二)
- 坚刚不可夺其志
- Palindrome Permutation
- C#之四十四 滑铁卢战役
- SpringMVC之安全性(一)
- 【Android 系统开发】CyanogenMod 13.0 源码下载 编译 ROM 制作 ( 手机平台 : 小米4 | 编译平台 : Ubuntu 14.04 LTS 虚拟机)
- SpringMVC之安全性(二)登录界面
- SpringMVC之安全性(三)Twitter登入
- SpringMVC小结(四)
- 结束SpringMVC
- 成为C++高手之头文件与条件编译
- Closest Binary Search Tree Value
- MySQL读取不重复的数据
- [最大密度子图 最小割] BZOJ 1312 Neerc2006 Hard Life
- Strobogrammatic Number