亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Security 自定義登錄頁面協助

Spring Security 自定義登錄頁面協助

萬千封印 2023-10-30 15:15:47
我一直在嘗試正確配置此配置,但是我遇到了一些困難,我相信這是一個我沒有看到的小修復。默認的 spring security 登錄頁面工作得很好,但我無法渲染自定義 html。它只是將獲取映射“login”讀取為字符串。任何幫助將不勝感激!1.安全配置@EnableWebSecuritypublic class SecurityConfiguration extends WebSecurityConfigurerAdapter {    @Autowired    UserDetailsService myUserDetailsServices;    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        auth.userDetailsService(myUserDetailsServices);    }    @Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().disable().        authorizeRequests()                .antMatchers("/admin").hasRole("ADMIN")                .antMatchers("/user").hasAnyRole("ADMIN", "USER")                .antMatchers("/").permitAll()                .and().formLogin()                    .loginPage("/login")                    .permitAll();    }    @Bean    public PasswordEncoder getPasswordEncoder() {        return NoOpPasswordEncoder.getInstance();    }}3.主控制器@RestController@CrossOrigin(origins="http://localhost:4200")public class MainController {    private UserRepository userRepository;    private UserDetailsService myUserDetailsServices;    public MainController(UserRepository userRepository) {        this.userRepository = userRepository;    }    @GetMapping("/")    public String home() { return ("<h1>Welcome</h1>");    }    @GetMapping("/login")    public String login() { return ("login");    }    @GetMapping("/user")    public String user() {        return ("<h1>Welcome User</h1>");    }    @GetMapping("/admin")    public String admin() {        return ("<h1>Welcome Admin</h1>");    }    @GetMapping("/all")    public @ResponseBody Iterable<User> getAllUsers() {        // This returns a JSON or XML with the users        return userRepository.findAll();
查看完整描述

3 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

我相信只要你嘗試一下就能解決這個問題。

authorizeRequests()
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers("/user").hasAnyRole("ADMIN", "USER")
                .antMatchers("/").permitAll()
                .and()   
                .formLogin().loginPage("/login.html")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/index.html",true);

另外,Spring security本身有映射url端點/login,如果你想改變它,那么方法loginProcessingUrl可以幫助改變登錄路徑


查看完整回答
反對 回復 2023-10-30
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

您的登錄頁面是 /login.html 而不是 /login


所以你的配置應該是:


.formLogin().loginPage("/login.html").permitAll()

.loginProcessingUrl("/login")

.defaultSuccessUrl("/index.html",true);

以及控制器中的 /login 映射。從 MainController 中刪除它:


 @GetMapping("/login")

    public String login() { return ("login");

    }


查看完整回答
反對 回復 2023-10-30
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

我想到了。我將此 ModelAndView 添加到我的主控制器類中并修復了它。不過還是謝謝你的建議。


    @RequestMapping("/login")

public ModelAndView login () {

    ModelAndView modelAndView = new ModelAndView();

    modelAndView.setViewName("login");

    return modelAndView;

}



查看完整回答
反對 回復 2023-10-30
  • 3 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號