您的位置:首页 > 编程语言 > Java开发

SpringMVC之安全性(一)

2016-06-06 05:55 417 查看
在前面的学习中,我们的系统并没的登录设置,这样对于我们系统来说是不安全的。我们需要指定用户或注册的用户可以登录我们系统。由于我们这个系统并没有用到数据库,我们以我就在代码中指定用户可以登录我们的系统。正常情况是要数据库来管理注册用户。

1.登录页面

使用SprigMVC来处理系统安全是非常快捷的,我们只添加依赖架包就可行了。在build.gradle的文件中添加下面的代码:

compile'org.springframework.boot:spring-boot-starter-security'

当你再次启动系统时,你就可以看到下面的界面。



好了,这要解决我人贵登录界面的问题我们不需要单独设置一个页面了。可是这个页面是如此不好看。在后面的章节中我们将会解决这个问题。

2.指定用户

我们可以指定多个用户登录我们的系统,所以我们需要在config的包下创建一个SecurityConfiguration的类。

package masterSpringMvc.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.
configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends
WebSecurityConfigurerAdapter {
@Autowired
public void configureAuth(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("user").roles("USER").and()
.withUser("admin").password("admin").roles("USER",
"ADMIN");
}
}

上面这段代码,当系统启动时将会记住我们系统的用户和对应的角色。它会重写安全的名字和安全密码(不是用Spring默认的)。

@EnableGlobalMethodSecurity注解将会允许我们去明确方法和类的安全的级别。

3.指定URL地址访问

注解可以让我们更容易去控制,但它是非常有效的。有时候,我们希望完全控制我们的授权。让我们先来看下面的代码。这里就是修改SecurityConfiguration类。

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends
WebSecurityConfigurerAdapter {
@Autowired
public void configureAuth(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("user").roles("USER").and()
.withUser("admin").password("admin").roles("USER",
"ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/login", "/logout").permitAll()
.antMatchers(HttpMethod.GET, "/api/**").hasRole("USER")
.antMatchers(HttpMethod.POST, "/api/**").hasRole("ADMIN")
.antMatchers(HttpMethod.PUT, "/api/**").hasRole("ADMIN")
.antMatchers(HttpMethod.DELETE, "/api/**").
hasRole("ADMIN")
.anyRequest().authenticated();
}
}

其实上面的代码中,我们添加了configue方法的具体逻辑处理。这里的意思就是,控制了以不同的URL地址请求进来的,我们规定的角色也是不一样的。

4.视图界面

可是我们只有登录进来的功能,那么我们想要退出该如何解决呢?Spring是否也提供别想包直接就可以解决呢?告诉你笔者没有发现有这个功能。我们将用Thymeleaf的框架来解决这个问题。下面我们添加视图defaul.html代码。在添加代码之前,我想我们应该先引用 thymeleaf提供的安全的控制的架包,我们先在build.gradle的文件中加入:

compile'org.thymeleaf.extras:thymeleaf-extras-springsecurity3'

引入了相应的包之后,我们在视图页面处理如下。

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extrasspringsecurity3">
<head>
<!-- content trimmed -->
</head>
<body>
<!-- content trimmed -->
<nav>
<div class="nav-wrapper indigo">
<ul class="right">
<!-- content trimmed -->
</ul>
</div>
</nav>
<div>
You are logged as <b sec:authentication="name" /> with roles <span
sec:authentication="authorities" />
-
<form th:action="@{/logout}" method="post" style="display: inlineblock">
<input type="submit" value="Sign Out" />
</form>
<hr/>
</div>
<section layout:fragment="content">
<p>Page content goes here</p>
</section>
<!-- content trimmed -->
</body>
</html>

5.总结

通过本节的学习,我们已经看了如何提高我们系统的安全,就是运用Spring的架包来处理用户登录的界面,这个界面默认并不是很好看。在下一章节我们将会做修改。同时,我们登录进系统后,我们也要实现退出的功能,这个功能我们是借用Thymeleaf的框架来实现的。最后我们运行的结果如下:



我们看到了我们最后加上去的视图代码的效果,可是这个提示需要在我们登录系统后应该要隐藏掉。笔者在网上查到的方法是修改上面的代码,修改情况如下,你可以试试看,笔者试了没有效果。

<div sec:authorize="isAuthenticated()">


源码下载:git@github.com:owenwilliam/masterSpringMVC6.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: