1-前端代码
login.html(2 KB)拷贝上述路径到下述位置,注意没有对应文件夹则需要手动创建2-后端代码1.定义接口这里我们在原来的基础之上,继续追加一个即可,我就简单的添加一下哥哥的地址吧
Java
运行代码复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.yzxb.SpringSecurity.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("demo")
public class HelloController {
@GetMapping
public String helloWorld() {
return "Hello Spring Security";
}
// 这个是新增的
@RequestMapping("/index")
public String index(){
return "油炸小波,欢迎加入IKUN大家庭";
}
}
2.创建配置类
Java
运行代码复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.yzxb.SpringSecurity.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;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated().and().formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/doLogin")
.defaultSuccessUrl("/demo/index")
.failureUrl("/login.html")
.usernameParameter("uname")
.passwordParameter("passwd")
.permitAll()
.and()
.csrf().disable();
}
}