SpringBoot中怎么整合SpringSecurity,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
鞍山網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)公司。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
@RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello"; } }
訪問http://localhost:8080/hello 會自動跳到login頁面
默認用戶名 user
默認密碼在控制臺顯示
spring.security.user.name=user spring.security.user.password=123 spring.security.user.roles=admin
新建一個SecurityConfig .java類
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //去掉spring5一定要密碼加密的限制 @Bean PasswordEncoder passwordEncoder(){ return NoOpPasswordEncoder.getInstance(); } //第二種: 在代碼中配置用戶名和密碼 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("terry").password("123").roles("admin") .and() .withUser("tt").password("456").roles("user"); } }
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //去掉spring5一定要密碼加密的限制 @Bean PasswordEncoder passwordEncoder(){ return NoOpPasswordEncoder.getInstance(); } //第二種: 在代碼中配置用戶名和密碼 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("terry").password("123").roles("admin") .and() .withUser("tt").password("456").roles("user"); } //HttpSecurity配置 @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("admin") .antMatchers("/user/**").hasAnyRole("admin","user") //.antMatchers("/user/**").access("hasAnyRole('user','admin')") .anyRequest().authenticated() .and() .formLogin() .loginProcessingUrl("/doLogin") .permitAll() .and() .csrf().disable();//要使用postman,防止被認為是csrf攻擊 } }
@RequestMapping("/admin/hello") public String admin(){ return "Hello admin"; } @RequestMapping("/user/hello") public String user(){ return "hello user"; }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
新聞標題:SpringBoot中怎么整合SpringSecurity
文章位置:http://bm7419.com/article42/pphphc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、響應式網(wǎng)站、網(wǎng)站排名、搜索引擎優(yōu)化、域名注冊、建站公司
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)