1.讲解一下各行的功能
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers("/css/**", "/js/**", "/fonts/**","/index","/register","/img/**").permitAll() // 一些静态文件和index页面,register页面可以直接访问,不受权限控制
.antMatchers("/admins/**").hasRole("ADMIN") // 需要相应的角色才能访问
.anyRequest().authenticated()//其他页面必须有权限才能访问
.and()
.formLogin() //基于 Form 表单登录验证
.loginPage("/login").failureUrl("/login-error").permitAll() // 自定义登录界面
.and().rememberMe().key(KEY) // 启用 remember me
.and().exceptionHandling().accessDeniedPage("/403"); // 处理异常,拒绝访问就重定向到 403 页面
http.headers().frameOptions().sameOrigin(); // 允许来自同一来源的H2 控制台的请求
}
2.页面中
sec:authorize="isAuthenticated()"
表示登录才可以显示
<div sec:authorize="isAuthenticated()" class="col-lg-5 col-xs-3" style="text-align: right" >
<form th:action="@{/logout}" method="post">
<input type="submit" class="btn btn-primary" value="注销"/>
</form>
</div>
sec:authorize="isAnonymous()"
表示未登录才可以显示
<div class="col-lg-5 col-xs-3" style="text-align: right" sec:authorize="isAnonymous()">
<button class="btn btn-secondary login" type="button">登录</button>
<button class="btn btn-secondary register" type="button">注册</button>
</div>
sec:authorize="hasRole('ROLE_ADMIN')"
表示指定用户才可以查看
<li sec:authorize="hasRole('ROLE_ADMIN')"><a href='/admin' >学生信息</a></li>
3.注销用户
在页面中
<form th:action="@{/logout}" method="post">
<input type="submit" class="btn btn-primary" value="注销"/>
</form>
在配置中加入
image.png
就可以了
网友评论